夯实spring(十):primary的使用和作用

妖狐艹你老母 2024-03-23 20:49 114阅读 0赞

当我们需要一个bean,但是spring容器帮我们找到了两个或多个时怎么办?当希望从容器中获取到一个bean对象的时候,容器中却找到了多个匹配的bean,出现这样的情况时会出现什么问题又怎么解决?

1,出现的问题

来一个案例:

Service接口

  1. public interface Service {
  2. }

两个实现类

  1. public class serviceImpl1 implements Service {
  2. }
  3. public class serviceImpl2 implements Service {
  4. }

上面代码,定义了一个接口Service,创建了两个类serviceImpl1 ,serviceImpl2 都实现了Service接口。

bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  6. <bean id="impl1" class="com.chen.serviceImpl1"></bean>
  7. <bean id="impl2" class="com.chen.serviceImpl2"></bean>
  8. </beans>

定义impl1和impl2两个bean

测试输出:

  1. public class Main {
  2. public static void main(String[] args) {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
  4. Service bean = context.getBean(Service.class);
  5. System.out.println(bean);
  6. }
  7. }
  8. Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.chen.Service' available: expected single matching bean but found 2: impl1,impl2
  9. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1273)
  10. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:494)
  11. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:349)
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
  13. at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1174)
  14. at com.chen.Main.main(Main.java:12)

可以看到输出错误,错误的原因是:spring容器中定义了2个bean,分别是impl1和impl2,这两个bean对象都实现了Service接口,而用例中我们想从容器中获取Service接口对应的bean,此时容器中有2个候选者(impl1和impl2)满足我们的需求,此时spring容器不知道如何选择,到底是返回impl1呢还是返回impl2呢?spring容器也不知道,所以报错了。

2,解决办法

上面的问题是当希望从容器中获取到一个bean对象的时候,容器中却找到了多个匹配的bean,此时spring不知道如何选择了,就会报这个异常。

spring中可以通过bean元素的primary属性来解决这个问题,可以通过这个属性来指定当前bean为主要候选者,当容器查询一个bean的时候,如果容器中有多个候选者匹配的时候,此时spring会返回主要的候选者。

我们把上面的bean.xml改成如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  6. <bean id="impl1" class="com.chen.serviceImpl1" primary="true"></bean>
  7. <bean id="impl2" class="com.chen.serviceImpl2"></bean>
  8. </beans>

此时输出:

  1. com.chen.serviceImpl1@73846619

可以看到正常输出了com.chen.serviceImpl1,因为com.chen.serviceImpl1置为主要候选者,spring就会将它返回。

总结:

  • 当从容器中查找一个bean的时候,如果容器中出现多个Bean候选者时,可以通过primary=”true”将当前bean置为首选者,那么查找的时候就会返回主要的候选者,否则将抛出异常。
  • 还有当候选者中如果有多个bean都将primary置为true,此时spring还是会不知道返回哪个的,也会报错,不知道如何选择了。

发表评论

表情:
评论列表 (有 0 条评论,114人围观)

还没有评论,来说两句吧...

相关阅读