Spring中类型自动装配--byType

在Spring中,“类型自动装配”的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它。
例如,一个“persion” bean 公开以“ability”类数据类型作为属性,Spring会找到ability类相同的数据类型,并自动装配它的Bean。如果没有匹配找到,它什么也不做。
 
package auto_w;

/**
* Created by luozhitao on 2017/8/8.
*/
public class ablity { public void setWrite_able1(String write_able1) {
this.write_able1 = write_able1;
} public String getWrite_able1() {
return write_able1;
} private String write_able1;
}
package auto_w;

/**
* Created by luozhitao on 2017/8/8.
*/
public class Person { public void setLity(ablity lity) {
this.lity = lity;
} public ablity getLity() {
return lity;
} private ablity lity;
}
package auto_w;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by luozhitao on 2017/8/8.
*/
public class p_app { public static void main(String [] args){ ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); Person person=(Person)context.getBean("person");
System.out.println(person.getLity().getWrite_able1()); }
}
 <bean id="person" class="auto_w.Person" autowire="byType"></bean>
<bean id="ablity" class="auto_w.ablity">
<property name="write_able1" value="write"/>
</bean>
上一篇:面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果


下一篇:java树形菜单实现