0
点赞
收藏
分享

微信扫一扫

Spring主动注入

624c95384278 2022-01-20 阅读 47
<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;
 }
举报

相关推荐

0 条评论