Using CustomAutowireConfigurer
CustomAutowireConfigurer
是 BeanFactoryPostProcessor
,它允许您注册您自己的自定义限定符注解类型,即使它们未使用 Spring 的 @Qualifier
注解进行注解。以下示例显示如何使用 CustomAutowireConfigurer
:
CustomAutowireConfigurer
is a BeanFactoryPostProcessor
that lets you register your own custom qualifier
annotation types, even if they are not annotated with Spring’s @Qualifier
annotation.
The following example shows how to use CustomAutowireConfigurer
:
<bean id="customAutowireConfigurer"
class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>example.CustomQualifier</value>
</set>
</property>
</bean>
AutowireCandidateResolver
通过以下方式确定自动装配候选对象:
The AutowireCandidateResolver
determines autowire candidates by:
-
The
autowire-candidate
value of each bean definition -
Any
default-autowire-candidates
patterns available on the<beans/>
element -
The presence of
@Qualifier
annotations and any custom annotations registered with theCustomAutowireConfigurer
当多个 Bean 满足自动装配候选对象的条件时,确定“主要”候选对象的方式如下:如果候选对象中恰好有一个 Bean 定义将 primary
属性设置为 true
,则选择该 Bean 定义。
When multiple beans qualify as autowire candidates, the determination of a “primary” is
as follows: If exactly one bean definition among the candidates has a primary
attribute set to true
, it is selected.