java Spring @Autowired 带有 2 个相同类型的 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3925681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:59:28  来源:igfitidea点击:

Spring @Autowired with 2 beans of the same type

javaspring

提问by DeliveryNinja

I have the following defined.

我有以下定义。

@Autowired
DaoType1<object1> someDao;

@Autowired
DaoType1<object1> someListDao;

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

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

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

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.

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

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

这是一个更简洁的版本。我忽略了其他 bean,因为它们与我无关。

applicationContext.xml

应用上下文.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 is not present in any of my xml files at all.

default-autowire 根本不存在于我的任何 xml 文件中。

回答by Bozho

This appears to be expected behaviour. The documentationsays:

这似乎是预期的行为。该文件说:

byName

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.

按名字

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

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

我想这意味着您已default-autowire="byName"在 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

但是,重构可能会以不可预测的方式影响这一点。这就是为什么(我认为)建议按类型切换到自动装配,并通过使用来消除 bean 的歧义

  • @Qualifier(as you noted)
  • @Resourcerather than @Autowired(as skaffman noted)
  • @Qualifier(正如你所指出的)
  • @Resource而不是@Autowired(如斯卡夫曼所指出的)

回答by Michael Wiles

The @Autowiredannotation behaves slightly differently to the "autowire by type" specification on xml based bean definitions.

@Autowired批注的行为略有不同,以对基于XML的bean定义中的“自动装配的类型”规范。

When using annotations you're not technically doing an auto wire... you're setting the value based on the annotation. The autowire annotation has the same function as the xml property element.

使用注释时,从技术上讲,您并不是在进行自动连线……而是根据注释设置值。autowire 注释与 xml 属性元素具有相同的功能。