使用 CustomAutowireConfigurer
CustomAutowireConfigurer
是一个 BeanFactoryPostProcessor,它允许你注册自己的自定义限定符注解类型,即使它们没有用 Spring 的 @Qualifier 注解进行标注。以下示例展示了如何使用 CustomAutowireConfigurer:
<bean id="customAutowireConfigurer"
class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>example.CustomQualifier</value>
</set>
</property>
</bean>
AutowireCandidateResolver 通过以下方式确定自动装配候选者:
-
每个 bean 定义的
autowire-candidate值 -
<beans/>元素上可用的任何default-autowire-candidates模式 -
@Qualifier注解以及注册到CustomAutowireConfigurer的任何自定义注解的存在
当多个 bean 符合自动装配候选条件时,对“primary”的确定如下:如果候选者中恰好有一个 bean 定义的 primary 属性设置为 true,则选择该定义。