欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring学习之四“自动装配”

程序员文章站 2022-05-24 14:38:06
...

一。根据名字自动装配:

前提一、设置default-autowire="byName"

前提二、id="bean5"和bean2中定义的有一个属性叫“bean5”一致

1、配置文件applicationContext-beans.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" 
           default-autowire="byName"          >
<bean id="bean2" class="com.bjsxt.spring.Bean2"/>
	 
	<bean  id="bean5" class="com.bjsxt.spring.Bean5">
		<property name="age" value="20"/>
	</bean>

 

2、配置文件applicationContext-other.xml

   <bean id="beanAbstract" abstract="true">
   		<property name="id" value="1000"/>
   		<property name="name" value="Jack"/>
   </bean>         
   
   <bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
   		<property name="name" value="Tom"/>
   		<property name="password" value="123"/>
   </bean>        
   
   <bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>

备注:省略部分bean.java代码:附上Bean2.java

public class Bean2 {

	private Bean3 bean3;
	
	private Bean4 bean4;
	
	private Bean5 bean5;

	public Bean3 getBean3() {
		return bean3;
	}

	public void setBean3(Bean3 bean3) {
		this.bean3 = bean3;
	}

	public Bean4 getBean4() {
		return bean4;
	}

	public void setBean4(Bean4 bean4) {
		this.bean4 = bean4;
	}

	public Bean5 getBean5() {
		return bean5;
	}

	public void setBean5(Bean5 bean5) {
		this.bean5 = bean5;
	}
}

 

3、测试代码:

public class AutowireTest extends TestCase {
	
	private BeanFactory factory;
	
	@Override
	protected void setUp() throws Exception {
		factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");	
	}

	public void testAutowire() {
		Bean2 bean2 = (Bean2)factory.getBean("bean2");
		System.out.println("bean2.bean3.id=" + bean2.getBean3().getId());
		System.out.println("bean2.bean3.name=" + bean2.getBean3().getName());
		System.out.println("bean2.bean3.password=" + bean2.getBean3().getPassword());
		System.out.println("bean2.bean4.id=" + bean2.getBean4().getId());
		System.out.println("bean2.bean4.name=" + bean2.getBean4().getName());
		System.out.println("bean2.bean5.age=" + bean2.getBean5().getAge());
	}
	
}

 

 

4、测试结果

bean2.bean3.id=1000
bean2.bean3.name=Tom
bean2.bean3.password=123
bean2.bean4.id=1000
bean2.bean4.name=Jack
bean2.bean5.age=20

 

一。根据类型自动装配:

前提一、设置default-autowire="byType"介意使用这个,名字写错了也不会错

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" 
          default-autowire="byType"
           >
	
	<!-- 
	<bean id="bean2" class="com.bjsxt.spring.Bean2">
		<property name="bean3" ref="bean3"/>
		<property name="bean4">
			<ref bean="bean4"/>
		</property>	
		<property name="bean5" ref="bean5"/>
	</bean>
	 -->
	
	<bean id="bean2" class="com.bjsxt.spring.Bean2"/>
	 
	<bean id="bean5111" class="com.bjsxt.spring.Bean5">
		<property name="age" value="20"/>
	</bean>
</beans>

 

<bean id="beanAbstract" abstract="true">
   		<property name="id" value="1000"/>
   		<property name="name" value="Jack"/>
   </bean>         
   
   <bean id="bean3222" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
   		<property name="name" value="Tom"/>
   		<property name="password" value="123"/>
   </bean>        
   
   <bean id="bean42222" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>

 

测试代码和测试结果和上面所述一致。