2016-08-05 14:29:32 杨家昌 阅读数 13400更多

分类专栏: spring
 
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

网上的文章已经很多了,这里就不说太多废话,开门见山。

@Autowired是spring的注解,默认使用的是byType的方式向Bean里面注入相应的Bean。例如:

@Autowired
private UserService userService;

这段代码会在初始化的时候,在spring容器中寻找一个类型为UserService的bean实体注入,关联到userService的引入上。

但是如果UserService这个接口存在多个实现类的时候,就会在spring注入的时候报错,具体如下:

public class UserService1 implements UserService
public class UserService2 implements UserService

当存多个UserService的实现类时,错误信息如下:

2016-08-05 14:53:53,795 ERROR [org.springframework.test.context.TestContextManager] - <Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@14a2f921] to prepare test instance [UserServiceTest@3c87521]>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private yjc.demo.service.UserService UserServiceTest.userService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [yjc.demo.service.UserService] is defined: expected single matching bean but found 2: userService1,userService2

抛出了org.springframework.beans.factory.BeanCreationException,而原因是注入的时候发现有2个匹配的bean,但是不知道要注入哪一个:expected single matching bean but found 2: userService1,userService2

那么如何应对多个实现类的场景呢,看一下代码:

@Autowired
private UserService userService1;
@Autowired
private UserService userService2;
@Autowired
@Qualifier(value = "userService2")
private UserService userService3; @Test
public void test(){
System.out.println(userService1.getClass().toString());
System.out.println(userService2.getClass().toString());
System.out.println(userService3.getClass().toString());
}

运行结果:

class yjc.demo.serviceImpl.UserService1
class yjc.demo.serviceImpl.UserService2
class yjc.demo.serviceImpl.UserService2

运行结果成功,说明了2种处理多个实现类的方法:

1.变量名用userService1,userService2,而不是userService。

通常情况下@Autowired是通过byType的方法注入的,可是在多个实现类的时候,byType的方式不再是唯一,而需要通过byName的方式来注入,而这个name默认就是根据变量名来的。

2.通过@Qualifier注解来指明使用哪一个实现类,实际上也是通过byName的方式实现。

由此看来,@Autowired注解到底使用byType还是byName,其实是存在一定策略的,也就是有优先级。优先用byType,而后是byName。

@Autowired注解到底是byType还是byName?的更多相关文章

  1. Autowired byType 与 byName 策略

    @Autowired是spring的注解,默认使用的是byType的方式向Bean里面注入相应的Bean.例如: @Autowiredprivate UserService userService;这 ...

  2. Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同

    前面说到了关于在xml中有提供default-autowire的配置信息,从spring 2.5开始,spring又提供了一个Autowired以及javaEE中标准的Resource注释,都好像可以 ...

  3. spring @Resource与@Autowired注解详解

    具有依赖关系的Bean对象,利用下面任意一种注解都可以实现关系注入: 1)@Resource (默认首先按名称匹配注入,然后类型匹配注入) 2)@Autowired/@Qualifier (默认按类型 ...

  4. Spring5:@Autowired注解、@Resource注解和@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  5. Spring中@Autowired注解、@Resource注解的区别

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

  6. 转:Spring中@Autowired注解、@Resource注解的区别

    Pay attention: When using these annotations, the object itself has to be created by Spring context. ...

  7. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  8. 04 Spring的@Autowired注解、@Resource注解、@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事务,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  9. @Resource与@Autowired注解的区别(转)

    Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解.如:@Resource.@PostConstruct及@PreDestroy 1.@Autowired ...

随机推荐

  1. Spring Boot集成Druid数据库连接池

    1. 前言 Druid数据库连接池由阿里巴巴开源,号称是java语言中最好的数据库连接池,是为监控而生的.Druid的官方地址是:https://github.com/alibaba/druid 通过 ...

  2. 【转载】UNICODE与ASCII的区别

    原文地址:https://blog.csdn.net/lx697/article/details/5914417 最近的项目涉及到了国际化的问题,由于之前并没有接触到UNICODE编码,因此,在项目期 ...

  3. nginx实现反向代理 配置多域名以及多端口号

    多个服务部署在同一个服务器,使用不同的端口. 一.思考过程 如果不想带上端口号,就只能访问 80 或 443 端口. 可以用过二级目录区分服务,通过反向代理转发到不同的 Server. 可以用过子域名 ...

  4. 两个概念:CCA和LDA

    典型相关性分析(CCA) https://blog.csdn.net/Mbx8X9u/article/details/78824216 典型关联分析(Canonical Correlation Ana ...

  5. The 2016 ACM-ICPC Asia China-Final L World Cup(深搜+回溯 暴力求解)

    题目分析: 对于A,B,C,D四支队伍,两两之间进行一场比赛,获胜得3分,平局得1分,失败不得分,现在对给出的四个队伍的得分,判断能否满足得到这种分数,且方案唯一输出yes,不唯一输出no,不可能则输 ...

  6. CodeForces 407E: k-d-sequence

    题目传送门:CF407E. 题意简述: 给定非负整数 \(k,d\) 和一个长度为 \(n\)(\(1\le n\le 2\times 10^5\))的整数序列 \(a\). 求这个序列中最长的一个连 ...

  7. java JSONObject

    JSONObject.has("key")方法首先判断是否含有该key字段,如果不存在该字段,返回false;如果存在此字段,还判断了该字段的value值是否为null,如果val ...

  8. POJ1463-Strategic game-(树形dp)

    http://poj.org/problem?id=1463 题意:有一棵n个结点的树,要在这棵树上放士兵守卫,一个士兵可以守卫自己所在的位置以及与之相邻的点.问最少放多少个士兵? 题解:对于每个点, ...

  9. SpringBoot配置文件加载位置与优先级

    1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 –fil ...

  10. 为什么两个一样的对象,用===打印是false

    对象的地址(变量名)存在栈中,对象的引用指向堆中,比较对象的时候,是比较对象的引用是否相等.obj和obj1的引用地址分别指向堆中的两块数据,所以不相等.