Spring @Autowired带有2个相同类型的bean [英] Spring @Autowired with 2 beans of the same type

查看:956
本文介绍了Spring @Autowired带有2个相同类型的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下内容.

@Autowired
DaoType1<object1> someDao;

@Autowired
DaoType1<object1> someListDao;

在我的bean定义中,我有两个相同类型的bean

and in my bean definitions I have two beans of the same type

<bean id="someDao" class="com.example.DaoType1" />
<bean id="someListDao" class="com.example.DaoType1" />

第二个bean从另一个xml文件中导入(如果有所不同).它们也设置了不同的属性.为什么spring不会抛出错误,因为已经定义了2个相同类型的bean.它是否使用变量名,因为它们与bean id匹配. dao不同,如果我对两个不同的bean使用@Qualifiers,则功能可以按预期工作.

The second bean is imported from another xml file if that makes a difference. They have different properties being set as well. Why is spring not throwing an error because 2 beans of the same type have been defined. Does it use the variable names since they match the bean ids. The dao's are different and the functionality works as expected if I had used @Qualifiers for the two different beans.

这是一个更简洁的版本.我没有其他豆了,因为我和它们无关.

Here is a more concise version. I've left out other beans since I they are not relevant.

applicationContext.xml

applicationContext.xml

<import resource="classpath:dm-services-crud.xml"/>
<bean id="ruleListCrudService" class="com.idna.dm.service.crud.impl.RuleCrudServiceImpl"> 
    <property name="crudDao" ref="ruleListCrudDao" />
</bean>

dm-services-crud.xml

dm-services-crud.xml

    <bean id="ruleCrudService" class="com.idna.dm.service.crud.impl.RuleCrudServiceImpl">
        <property name="crudDao" ref="ruleCrudDao" />
        <property name="ruleNetworkOfNodesCrudService" ref="ruleNetworkOfNodesCrudService" />
        <property name="elementMappingsCrudService" ref="elementMappingsCrudService" />
        <property name="ruleCrudDao" ref="newRuleCrudDao"/>
   </bean>

default-autowire根本不在我的任何xml文件中.

default-autowire is not present in any of my xml files at all.

推荐答案

这似乎是预期的行为. 文档说:

This appears to be expected behaviour. The documentation says:

byName

byName

按属性名称自动装配. Spring寻找与需要自动装配的属性同名的bean.例如,如果一个bean定义被设置为按名称自动装配,并且包含一个master属性(即,它具有setMaster(..)方法),则Spring将查找一个名为master的bean定义,并使用它来设置属性.

Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property.

我想这意味着您已经在applicationContext.xml中指定了default-autowire="byName".

I guess this means you have specified default-autowire="byName" in your applicationContext.xml.

但是,重构可能会以不可预测的方式影响它.这就是为什么(我认为)建议您按类型切换到自动装配,并使用

However, refactoring may affect this in an unpredictable way. That's why (I think) it is advisable to switch to autowiring by type, and disambiguate the beans by the use of

  • @Qualifier(如您所述)
  • @Resource而不是@Autowired(如skaffman所述)
  • @Qualifier (as you noted)
  • @Resource rather than @Autowired (as skaffman noted)

这篇关于Spring @Autowired带有2个相同类型的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆