<bean id="cat" class="pojo.Cat"/>
<bean id="dog" class="pojo.Dog"/>
<bean id="people" class="pojo.People" autowire="byName"/>
autowire=“byName”
会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!
autowire=“byType”
会自动在容器上下文中查找,和自己对象属性类型相同的bean
但必须保证该类全局唯一
<bean class="pojo.Cat"/>
<bean class="pojo.Dog"/>
<bean id="people" class="pojo.People" autowire="byType"/>
@Autowired自动注解,还可以配套@Qualifier(value=“xxx”)使用,指定唯一的bean对象注入
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
}
或者@Resource注解,其功能和@Autowired一样
public class People{
@Resource(name="cat2")
private Cat cat;
@Resouce
private Dog dog;
}