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
- spring学习(官网)——解决spring有多个匹配bean产生的冲突
- 解决spring的bean同名冲突
- spring学习(官网)——解决版本冲突问题
- spring学习笔记叁——spring的bean注解
- spring学习笔记(2)——bean的配置
- spring学习笔记(9)——bean的生命周期
- spring的bean工厂的产生
- spring的bean工厂的产生
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory产生Bean的方法
- Spring中BeanFactory产生Bean的方法
- Beginning Spring学习笔记——第2章(三)Spring的Bean管理
- Spring(二)——Spring管理Bean的原理
- 3、spring入门—Spring Bean的装配(上)
- 4、spring入门—Spring Bean的装配(下)
- 解决多spring框架共存产生的webapp.root冲突错误
- spring学习(-)关于bean的生命周期
- Spring学习----Bean的装配(4)
- 建立可视化决策平台,“数据化”首当其冲!
- appverifier
- SQL:将表中生日字段转化为年龄
- platform 架构
- mac出现错误“ld: symbol(s) not found for architecture x86_64”
- spring学习(官网)——解决spring有多个匹配bean产生的冲突
- 第一行代码Android学习笔记2——在活动中使用Menu但程序运行时找不到menu选项
- Android 架构随想之配置文件
- 第五周--后缀表达式
- hive.exec.parallel.thread.number
- 第五周项目—斐波那契数列
- [Highcharts] 初识Highcharts 饼状图示例
- [今日头条] 笔试题目2016/9/28
- JIT