Autowired byType 与 byName 策略
@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。
原文:https://blog.csdn.net/yangjiachang1203/article/details/52128830
Autowired byType 与 byName 策略的更多相关文章
- @Autowired注解到底是byType还是byName?
2016-08-05 14:29:32 杨家昌 阅读数 13400更多 分类专栏: spring 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明 ...
- Spring注入中byType和byName的总结
1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...
- Spring autowire自动装配 ByType和ByName
不使用自动装配前使用的是类的引用: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- @Autowired
1. Spring框架中进行注入式,使用@Autowired. @Autowired可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作,这里必须明确:@Autowired是根据类型进行自动装 ...
- Spring注解@Resource和@Autowired区别对比
转载:http://www.cnblogs.com/think-in-java/p/5474740.html @Resource和@Autowired都是做bean的注入时使用,其实@Resource ...
- Spring中@Autowired注解、@Resource注解的区别
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- @Autowired @Resource用法
@Autowired的用法和作用 这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="u ...
- 转:Spring中@Autowired注解、@Resource注解的区别
Pay attention: When using these annotations, the object itself has to be created by Spring context. ...
- 【Spring学习笔记-6】关于@Autowired与@Scope(BeanDefination.SCOPE_PROTOTYPE)
当类被@Scope(BeanDefination.SCOPE_PROTOTYPE)修饰时,说明每次依赖注入时,都会产生新的对象,具体可参见文章:http://blog.csdn.net/gst6062 ...
随机推荐
- ubuntu安装jdk,maven,tomcat
ubuntu16.04安装jdk8 -jdk 检查是否安装成功 java -version 出现如上信息即安装成功 安装maven,先去官网下载指定版本的maven,个人使用apache-maven- ...
- SpringMVC处理XML格式的数据
1.搭建SpringMVC+spring环境 2.web.xml,Springmvc-config.xml.springMVC提供了处理xml格式请求响应的HttpMessageConverter,s ...
- 解决find命令报错: paths must precede expression
eg: find . -name *.c -or -name *.cpp 需要将模糊搜索词用引号括起来: find . -name "*.c" -or -name "*. ...
- mysql学习笔记--数据库设计
一.数据库基本概念 1. 关系:两个表的公共字段 2. 行:也称记录,也称实体 3. 列:也称字段,也称属性 4. 数据冗余:相同的数据存在不同的地方. 注意:冗余只能减少,不能杜绝. 减少冗余,只能 ...
- 245. Shortest Word Distance III 单词可以重复的最短单词距离
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- [leetcode]339. Nested List Weight Sum嵌套列表加权和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- node.js中Buffer缓冲器的使用
一.什么是Buffer Buffer缓冲器是用来存储输入和输出数据的一段内存.js语言没有二进制数据类型,在处理TCP和文件流的时候,就不是很方便了. 所以node.js提供了Buffer类来处理二进 ...
- Git 在团队中的使用--如何正确使用Git Flow
Git的优点 Git的优点很多,但是这里只列出我认为非常突出的几点. 由于是分布式,所有本地库包含了远程库的所有内容. 优秀的分支模型,打分支以及合并分支,机器方便. 快速,在这个时间就是金钱的时代, ...
- 16. pt-mysql-summary
pt-mysql-summary --host=192.168.100.101 --port=3306 --user=admin --password=admin \ pt-mysql-summary ...
- Innodb IO优化-配置优化
作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究. 对于数据库来讲 ...