Spring的注解@Qualifier小结
有以下接口:
- public interface EmployeeService {
- public EmployeeDto getEmployeeById(Long id);
- }
有两个实现类:
- @Service("service")
- public class EmployeeServiceImpl implements EmployeeService {
- public EmployeeDto getEmployeeById(Long id) {
- return new EmployeeDto();
- }
- }
- @Service("service1")
- public class EmployeeServiceImpl implements EmployeeService {
- public EmployeeDto getEmployeeById(Long id) {
- return new EmployeeDto();
- }
- }
调用方法为:
- @Controller
- @RequestMapping("/emplayee")
- public class EmployeeInfoControl {
- @Autowired
- EmployeeService employeeService;
- @RequestMapping(params = "method=showEmplayeeInfo")
- public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
- #略
- }
- }
报错
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeInfoControl':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.test.service.EmployeeService com.test.controller.EmployeeInfoControl.employeeService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [com.test.service.EmployeeService] is defined: expected single matching bean but found 2: [service1, service2]
很明显了,在autoware时,由于有两个类实现了EmployeeService接口,所以Spring不知道应该绑定哪个实现类,所以抛出了如上错误。
这个时候就要用到@Qualifier注解了,qualifier的意思是合格者。通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称必须为我们之前定义@Service注解的名称之一。
- @Controller
- @RequestMapping("/emplayee")
- public class EmployeeInfoControl {
- @Autowired
- @Qualifier("service")
- EmployeeService employeeService;
- @RequestMapping(params = "method=showEmplayeeInfo")
- public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
- #略
- }
- }
Spring的注解@Qualifier小结的更多相关文章
- Spring的注解@Qualifier用法
Spring的注解@Qualifier用法在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?根据注入资源的注解不同实现的方式有一点小小的区别 ...
- Spring的注解@Qualifier(二十五)
转载:https://www.cnblogs.com/smileLuckBoy/p/5801678.html 近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明 ...
- Spring的注解@Qualifier
近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...
- Spring Conditional注解使用小结
今天我们来总结下Conditional注解的使用. Conditional注解 增加配置类Config package condition; import org.springframework.co ...
- Spring的注解@Qualifier注解
@Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称 ...
- spring boot常用注解使用小结
1.@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解. 4.0之前的版本,Sprin ...
- Spring JSR-250注解
Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...
- Spring的注解问题
Annotation(注解)概述 从JDK5.0开始, Java增加了对元数据(MetaData)的支持,也就是 Annotation(注解). Annotation其实就是代码里的特殊标记,它用于替 ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
随机推荐
- javascript 中的类型
javascript 中的类型 js 是一门弱语言,各式各样的错误多种多样,特别是确定返回值有问题的时候,你会用什么来进行表示错误? 我一般有三个选择: null '' error {} 第一个选择 ...
- Python-字符串及列表操作-Day2
1.数据类型 1.1 变量引出数据类型 变量:用来记录状态变量值的变化就是状态的变化,程序运行的本质就是来处理一系列的变化 1.2 五大基本数据类型: 数字 字符串 列表 元组 字典 1.2.1 数字 ...
- kubernetes入门(01)kubernetes是什么?
一.kubernetes是什么? Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署.大规模可伸缩.应用容器化管理.在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以 ...
- SpringCloud的Hystrix(二) 某消费者应用(如:ui、网关)访问的多个微服务的断路监控
一.验证断路保护监控是否管理多个消费者 app 1.第1个消费者应用:访问自己封装rest服务 saleProd 2.第2个消费者应用:第二个应用没有反应 说明 1.每个应用实例的断路保护机制,只对本 ...
- zuul入门(5)zuul 处理异常
Object accessToken = request.getParameter("accessToken"); if(accessToken==null) { // 设置zuu ...
- Python之协程
前言 在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来说我们已经算是把cpu的利用率提高很多了.但是我们知道无论是创建多进程还是创建多线程来解决问题,都要消耗一定的时间来创建 ...
- 判断ssh远程命令是否执行结束
注:这是一个没什么鸟用的功能.不过也算是一种拓展. 通常在那些"一键化部署"的shell脚本中,可能需要使用ssh执行远程命令来实现一些简单的自动化,这些远程命令可能需要执行一段时 ...
- Hibernate(三): org.hibernate.HibernateException: No CurrentSessionContext configured!
Hibernate版本5.2.9 获取Session的方式是sessionFactory.getCurrentSession(); 比较老一些的版本使用的是sessionFactory.openSes ...
- POJ-2234 Matches Game---尼姆博奕裸题
题目链接: https://vjudge.net/problem/POJ-2234 题目大意: 尼姆博奕裸题 思路: 直接异或 #include<iostream> #include< ...
- Oracle数据库(3-7)
显式游标使用主要有四个步骤: 声明/定义游标打开游标读取数据关闭游标 CASE 条件表达式 WHEN 条件表达式结果1 THEN 语句1 WHEN 条件表达式结果2 THEN 语句2 ...... W ...