spring学习(官网)——解决spring有多个匹配bean产生的冲突

来源:互联网 发布:生日邀请函制作软件 编辑:程序博客网 时间:2024/04/19 16:52

1:使用@Primary注解微调:

通过类型自动匹配可能会导致多个候选对象,在选择过程中有更多限制是必须的,一种方式是使用spring 的@Primary注解实现,当一个单值的依赖有多个候选的bean时,@Primary注解指示给予一个优先的bean。如果在所有候选的bean中只有一个是primary的,这个bean将会被匹配。

@Configurationpublic class MovieConfiguration {    @Bean    @Primary    public MovieCatalog firstMovieCatalog() { ... }    @Bean    public MovieCatalog secondMovieCatalog() { ... }    // ...}

public class MovieRecommender {    @Autowired    private MovieCatalog movieCatalog;    // ...}

MovieRecommender将会被匹配为firstMovieCatalog。相应的bean定义:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <context:annotation-config/>    <bean class="example.SimpleMovieCatalog" primary="true">        <!-- inject any dependencies required by this bean -->    </bean>    <bean class="example.SimpleMovieCatalog">        <!-- inject any dependencies required by this bean -->    </bean>    <bean id="movieRecommender" class="example.MovieRecommender"/></beans>

基于注解的可以在类上使用@Component  @Service等注解注册bean,然后在优先的类上添加@Primary注解;

当在选择处理上有更多限制,可以使用spring @Qualifier注解,你可以使用给定的参数关联qualifier值,

public class MovieRecommender {    @Autowired    @Qualifier("main")    private MovieCatalog movieCatalog;    // ...}

@Qualifier注解也可以在构造器参数和方法参数上指定:

public class MovieRecommender {    private MovieCatalog movieCatalog;    private CustomerPreferenceDao customerPreferenceDao;    @Autowired    public void prepare(@Qualifier("main")MovieCatalog movieCatalog,            CustomerPreferenceDao customerPreferenceDao) {        this.movieCatalog = movieCatalog;        this.customerPreferenceDao = customerPreferenceDao;    }    // ...}

相应的bean定义,构造器参数Qualifier值和bean的Qualifier值一致的话,对应bean将被匹配:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <context:annotation-config/>    <bean class="example.SimpleMovieCatalog">        <qualifier value="main"/>        <!-- inject any dependencies required by this bean -->    </bean>    <bean class="example.SimpleMovieCatalog">        <qualifier value="action"/>        <!-- inject any dependencies required by this bean -->    </bean>    <bean id="movieRecommender" class="example.MovieRecommender"/></beans>

CustomAutowireConfigurer是一个BeanFactoryPostProcessor允许你注册自定义的qualifier类型,即使他们没有被spring的@Qualifier注解标识。

<bean id="customAutowireConfigurer"        class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">    <property name="customQualifierTypes">        <set>            <value>example.CustomQualifier</value>        </set>    </property></bean>



0 0
原创粉丝点击