跳到主要内容

使用 CustomAutowireConfigurer

ChatGPT-4o 中英对照 Using CustomAutowireConfigurer Using 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>
xml

AutowireCandidateResolver 通过以下方式确定自动装配候选项:

  • 每个 bean 定义的 autowire-candidate

  • <beans/> 元素上可用的任何 default-autowire-candidates 模式

  • @Qualifier 注解的存在以及通过 CustomAutowireConfigurer 注册的任何自定义注解

当多个 bean 符合自动装配候选条件时,“主要” bean 的确定如下:如果候选者中恰好有一个 bean 定义的 primary 属性设置为 true,则选择该 bean。