Spring的继续———基础功能的实现(1)

178 阅读4分钟
  • 基本类型的装配 方式:

      配置元素<value/>
      例子:
      
          public class HelloBean {
          private String name;
          private int age;
          public String sayHello(){
      	return "hello "+name +",your age is" + age;
          }
          .............
          }
          配置文件set.xml
    
          <bean id="helloBean" class="ioc.HelloBean">
          <property name="name">
      	<value>tom</value>
          </property>
          <property name="age" value="20">
          </property>
          </bean>
          
          id是Bean的唯一标识,要求在整个配置文件中要唯一,也可使用name属性,bean标签里面的id和name属性都可以用来标识这个配置的对象,
          但是id会帮我们检查给对象起的名字是否规范(名字不能重复、不能用数字开头、不能有空格等等),如果检查出来了那么就会报错name
          属性不会帮检查这些东西
          -->
          <!--property 对于所有用set方式来注入的必须使用该标签-->
          <!--value 是对于基本类型,都用value(标签/属性)来注入,可以实现自动的数据类型转换-->
          测试类:
          main:
          ApplicationContext ac = 
      	new ClassPathXmlApplicationContext("set.xml");
          //获取容器的一个实例
      	HelloBean hb = (HelloBean) ac.getBean("helloBean");
          System.out.println(hb.sayHello());
    
  • 对象类型的装配 1. 用于涉及的对象的id在本配置文件中 2. 用于涉及的对象的id不在本配置文件中 3.使用property的ref属性引用

例如:
public class OtherBean {
	private String str1;
	public String getStr1() {
		return str1;
	}
	public void setStr1(String str1) {
		this.str1 = str1;
	}
	public String toString(){
		return "OtherBean "+str1;
	}
}

public class SomeBean {
	private OtherBean ob;
	public void printInfo(){
		System.out.println("someBean "+ob);
	}
	public OtherBean getOb() {
		return ob;
	}
	public void setOb(OtherBean ob) {
		this.ob = ob;
	}
}

配置applicationContext.xml
<bean id="someBean" class="ioc.SomeBean">
	<property name="ob">
	    <!--  <ref bean=" "/>  用于涉及的对象的id不在本配置文件中 -->
		<ref bean="otherBean" />
	</property>
</bean>

配置other.xml文件
<bean id="otherBean" class="ioc.OtherBean">
	<property name="str1">
		<value>hello</value>
	</property>
</bean>

测试类:
main:
	String[] path = {"ioc/applicationContext.xml","ioc/other.xml"};
	ApplicationContext ac = new ClassPathXmlApplicationContext(path);
	SomeBean sb = (SomeBean) ac.getBean("someBean");
	sb.printInfo();
  • 集合的装配 方式:配置元素

       例如:
    
public class SomeBean {
	private List listProperty;
	private Set setProperty;
	private Map mapProperty;
	private Properties<String,String> property;
	public List getListProperty() {
		return listProperty;
	}
	public void setListProperty(List listProperty) {
		this.listProperty = listProperty;
	}
	public Set getSetProperty() {
		return setProperty;
	}
	public void setSetProperty(Set setProperty) {
		this.setProperty = setProperty;
	}
	public Map getMapProperty() {
		return mapProperty;
	}
	public void setMapProperty(Map mapProperty) {
		this.mapProperty = mapProperty;
	}
	public Properties getProperty() {
		return property;
	}
	public void setProperty(Properties property) {
		this.property = property;
	}
	public void printInfo(){
		System.out.println("listProperty");
		System.out.println(listProperty);
		System.out.println("setProperty");
		System.out.println(setProperty);
		Set set = mapProperty.entrySet();
		Iterator it = set.iterator();
		while(it.hasNext()){
			 Map.Entry entry = (Entry) it.next();
			 System.out.println("Key " +entry.getKey() );
			 System.out.println("value "+entry.getValue());
		}
		System.out.println("props: ");
		Set set2 = property.entrySet();
		Iterator it2 = set2.iterator();
		while(it2.hasNext()){
			Map.Entry entry= (Entry) it2.next();
			System.out.println("key "+entry.getKey());
			System.out.println("value "+entry.getValue());
		}
	}
}

applicationContext.xml的写法:
<bean id="someBean" class="ioc.SomeBean">
	<property name="listProperty">
		<list>
			<value>list1</value>
		        <value>list2</value>
		        <value>list3</value>
		</list>
	</property>
	<property name="setProperty">
		<set>
		    <value>set1</value>
		    <value>set2</value>
		    <value>set3</value>
		</set>
	</property>
	<property name="mapProperty">
		<map>
		    <entry key="key1">
			  <value>value1</value>
		    </entry>
		    <entry key="key2">
			  <value>value2</value>
		    </entry>
		</map>
	</property>

	<property name="property">
	    <props>
		  <prop key="key1">prop1</prop>
		  <prop key="key2">prop2</prop>
		  <prop key="key3">prop3</prop>
	    </props>
	</property>
</bean>

测试类:
main:
	String path = "ioc/applicationContext.xml";
	ApplicationContext  ac =
		new ClassPathXmlApplicationContext(path);
	SomeBean sb = (SomeBean) ac.getBean("someBean");
	sb.printInfo();
  • 2.基于构造器注入 方式: 配置元素 在Bean中不用写set方法,但是要有相应的构造器

      构造器注入有俩种形式 一个是根据参数类型 一个是根据参数位置的下标
      <constructor-arg type="int" value="">
      <constructor-arg  index="0" value="">
    

    例如:

     <bean name="student" class="com.briup.bean.Student">
      <constructor-arg type="int" value="25">
      </constructor-arg>
      
      <constructor-arg type="java.lang.String" value="tom">
      </constructor-arg>
      
      <constructor-arg type="long" value="100">
      </constructor-arg>
    

    或者:

      <bean name="student" class="com.briup.bean.Student">
      <constructor-arg index="2">
      	<value>30</value>
      </constructor-arg>
       
      <constructor-arg index="0">
      	<value>200</value>
      </constructor-arg>
       
      <constructor-arg index="1">
      	<value>lily</value>
      </constructor-arg>
    
  • 3.自动注入 :容器依照一些规则去装配bean中的一个属性 注意:自动装配只对[对象类型]起作用,对基本类型不起作用.

      第一种情况:
      	在beans标签中配置装载方式:default-autowire="byName"
    
      	在根元素beans中加入这个属性,那么下面所有的bean都会
      	使用byName的方式进行自动注入,如果在下面的某一个bean
      	里面想使用其他的方式进行注入,可以用autowire=""属性进行
      	说明,或者某一个bean不想使用任何自动注入就使用autowire="no"
    
      第二种情况:
      	在bean标签中指定配置方式
    
      	autowire="byName":
      	spring容器会到当前的类中找property的名字,然后
      	再根据这个名字去spring容器中找有没有和这个property
      	名字相同的对象,有的话,就把这个对象当做参数放到
      	setXxxx这个方法里面注入进来.
      	注意:了解property指的类中的什么东西
    
    
      	autowire="byType":
      	spring容器会根据当前类中的set方法里面参数的类型,
      	去容器中找相匹配的对象,如果没找到就算了,如果找到
      	一个就注入进来,如果找到多个,那么就会报错了.
    
    
      	autowire="constructor" 
      根据构造器的参数类型去匹配