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. idea svn 文件还原到指定版本

    使用svn经常会遇见,由于自己或者其他同事的提交(或者误操作),把原本正确代码或文件覆盖掉了,现在就得需要恢复到之前指定的某个版本. 先打开指定的文件,然后右上角点击时间的按钮: 这比会出现以前的许多 ...

  2. 源码安装rlwrap 0.43(为了方便使用linux下的sqlplus)

    为了linux下的sqlplus方便调用历史命令和退格,安装下rlwrap,最新版本是0.43,貌似作者已经不更新了 下载地址 https://fossies.org/linux/privat/rlw ...

  3. Python的诞生和各种解释器

    一:Python的诞生 参考:https://www.jianshu.com/p/1cc1382e5e04   二:Python的各种解释器 参考:https://www.liaoxuefeng.co ...

  4. WebVTT字幕格式

    [时间:2019-05] [状态:Open] [关键词:字幕,vtt,webvtt, 文件格式,cue,css] 0 引言 WebVTT(Web Video Text Tracks),通过HTML5中 ...

  5. Mock Server之与被测系统对接(python+flask)

    第一步:获取入参与返回结果 先通过postman.jmeter.自己写脚本之类的方式请求我们的mock server,试着获取入参与对应的返回值,这里我用的是robotframework + Requ ...

  6. vue - 基础(2)

    <div id="content"> {{ msg }} <div v-text="msg"></div> <div ...

  7. 04-C#笔记-数据类型转化

    支持强制类型转换. 常用的转化函数如下: 1 ToBoolean如果可能的话,把类型转换为布尔型. 2 ToByte把类型转换为字节类型. 3 ToChar如果可能的话,把类型转换为单个 Unicod ...

  8. python递归和内置方法

    递归:函数调用自身 核心:递进的时候能够达到一个结果,问题规模越来越小(不一定要真正的达到):设置一个条件,能够让最后一次函数调用结束 练习: ​ 第一个人的姓名是16岁,后面每个人的年龄都比前一个大 ...

  9. VIJOS-P1066 弱弱的战壕

    JDOJ 1247: VIJOS-P1066 弱弱的战壕 题目传送门 Description 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了--b. mx在他的基地附近建立了 ...

  10. 每天一道Rust-LeetCode(2019-06-01)

    每天一道Rust-LeetCode(2019-06-01) 坚持每天一道题,刷题学习Rust. 题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的, ...