Spring Bean中的自动装配——byType
自动装配byType即通过查找类属性在配置文件中bean中定义的class属性来注入,而不是通过类属性名与配置文件中bean的id属性来匹配的。
如下例:
package com.lwf.bean;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;}}?
package com.lwf.bean;import java.util.Date;public class Bean3 {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}?
package com.lwf.bean;public class Bean4 {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}?
package com.lwf.bean;public class Bean5 {private int age;public int getAge() {return age;}public void setAge(int age) {this.age = age;}}?
配置文件:
<?xml version="1.0" encoding="UTF-8"?><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.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"default-autowire="byType"><bean id="bean2" value="33"></property></bean><bean id="parentBean" abstract="true"><property name="id" value="1"/><property name="name" value="zhang"/></bean><bean id="bean3ref" parent="parentBean"/><bean id="bean4ref" parent="parentBean"/></beans>
?
可以看到配置文件改为byType。
测试类:
package com.lwf.client;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lwf.bean.Bean2;public class Client {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext*.xml");Bean2 bean2 = (Bean2)ctx.getBean("bean2");System.out.println(bean2.getBean3().getName());System.out.println(bean2.getBean3().getId());System.out.println(bean2.getBean4().getId());System.out.println(bean2.getBean4().getName());System.out.println(bean2.getBean5().getAge());}}?
?
注:在这次测试中,Bean3中删除了Date属性,这次因为在测试的时候发现自己写的日期转化类在byType下报错。
本次测试结果:
2010-05-19 17:08:39,732 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19fcc69: display name [org.springframework.context.support.ClassPathXmlApplicationContext@19fcc69]; startup date [Wed May 19 17:08:39 CST 2010]; root of context hierarchy2010-05-19 17:08:39,888 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\workdirlocal\spring_autowire\bin\applicationContext.xml]2010-05-19 17:08:40,201 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@19fcc69]: org.springframework.beans.factory.support.DefaultListableBeanFactory@20be792010-05-19 17:08:40,263 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@20be79: defining beans [bean2,bean5ref,parentBean,bean3ref,bean4ref]; root of factory hierarchyzhang11zhang33
?