简体   繁体   English

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

[英]Spring @Autowired with 2 beans of the same type

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. 第二个bean是从另一个xml文件导入的,如果这有所不同的话。 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. 为什么spring没有抛出错误,因为已经定义了2个相同类型的bean。 Does it use the variable names since they match the bean ids. 它是否使用变量名称,因为它们匹配bean ID。 The dao's are different and the functionality works as expected if I had used @Qualifiers for the two different beans. 如果我将@Qualifiers用于两个不同的bean,那么dao是不同的,并且功能按预期工作。

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-服务,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文件中。

This appears to be expected behaviour. 这似乎是预期的行为。 The documentation says: 文件说:

byName 按名字

Autowiring by property name. 按属性名称自动装配。 Spring looks for a bean with the same name as the property that needs to be autowired. Spring查找与需要自动装配的属性同名的bean。 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. 例如,如果bean定义按名称设置为autowire,并且它包含master属性(即,它具有setMaster(..)方法),则Spring会查找名为master的bean定义,并使用它来设置属性。

I guess this means you have specified default-autowire="byName" in your applicationContext.xml. 我想这意味着你已经在applicationContext.xml中指定了default-autowire="byName"

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) @Qualifier (正如你所说)
  • @Resource rather than @Autowired (as skaffman noted) @Resource而不是@Autowired (正如skaffman所说)

The @Autowired annotation behaves slightly differently to the "autowire by type" specification on xml based bean definitions. @Autowired注释与基于xml的bean定义的“按类型的autowire”规范略有不同。

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属性元素具有相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM