【发布时间】:2010-10-13 16:06:34
【问题描述】:
我定义了以下内容。
@Autowired
DaoType1<object1> someDao;
@Autowired
DaoType1<object1> someListDao;
在我的 bean 定义中,我有两个相同类型的 bean
<bean id="someDao" class="com.example.DaoType1" />
<bean id="someListDao" class="com.example.DaoType1" />
第二个 bean 是从另一个 xml 文件中导入的,如果那样的话。它们也设置了不同的属性。为什么 spring 没有抛出错误,因为已经定义了 2 个相同类型的 bean。它是否使用变量名称,因为它们与 bean id 匹配。如果我对两个不同的 bean 使用了 @Qualifiers,则 dao 是不同的,并且功能按预期工作。
这是一个更简洁的版本。我省略了其他 bean,因为它们不相关。
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
<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 文件中。
【问题讨论】:
-
嗯,我原以为会失败。也许它确实使用了字段名称。这就是它对
@Resource所做的事情,所以这将是一致的。 -
你能显示整个
applicationContext.xml(两者)吗?