近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~

先说明下场景,代码如下:

有如下接口:

public interface EmployeeService {
public EmployeeDto getEmployeeById(Long id);
}

同时有下述两个实现类 EmployeeServiceImpl和EmployeeServiceImpl1:

@Service("service")
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeDto getEmployeeById(Long id) {
return new EmployeeDto();
}
} @Service("service1")
public class EmployeeServiceImpl1 implements EmployeeService {
public EmployeeDto getEmployeeById(Long id) {
return new EmployeeDto();
}
}
 

调用代码如下:

@Controller
@RequestMapping("/emplayee.do")
public class EmployeeInfoControl { @Autowired
EmployeeService employeeService; @RequestMapping(params = "method=showEmplayeeInfo")
public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
#略
}
}

在启动tomcat时报如下错误:

 
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.do")
public class EmployeeInfoControl { @Autowired
@Qualifier("service")
EmployeeService employeeService; @RequestMapping(params = "method=showEmplayeeInfo")
public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
#略
}
}

问题解决!

【spring】@Qualifier注解的更多相关文章

  1. spring @qualifier注解

    1.spring @qualifier注解用来在spring按类型装配可能存在多个bean的情况下,@qualifier注解可以用来缩小范围或者指定唯一. 也可以用来指定方法参数 2.@qualifi ...

  2. spring @Qualifier注解使用

    @Autowired是根据类型进行自动装配的.如果当Spring上下文中存在多个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在U ...

  3. Spring学习(10)--- @Qualifier注解

    按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数 可用于注解集合类型变量 例子: package c ...

  4. Spring的注解@Qualifier小结

    有以下接口: public interface EmployeeService { public EmployeeDto getEmployeeById(Long id); } 有两个实现类: @Se ...

  5. 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier

    Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...

  6. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

    Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...

  7. [Spring]@Autowired,@Required,@Qualifier注解

    @Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...

  8. Spring的注解@Qualifier(二十五)

    转载:https://www.cnblogs.com/smileLuckBoy/p/5801678.html 近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明 ...

  9. Spring的注解@Qualifier

    近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...

随机推荐

  1. Java中 单例(Singleton)的两种方式

    第一种(饿汉式单例模式):在声明变量时实例化 public class Singleton { //静态初始化自动实例化 private static Singleton instance = new ...

  2. 只能在执行Render() 的过程中调用 RegisterForEventValidation;

    October 17, 2008  3:28 PMMarch 29, 2013  8:28 PM Aillo 编程   0 Comments 在实现"将GridView中的数据导出到Exce ...

  3. 【开发工具】最强Git使用总结

    目录 必读材料 常用小结 Git操作流程 Git常用操作命令 - 代码提交和同步代码 Git常用操作命令 - 代码撤销和撤销同步 Git常用操作命令 - 其它常用命令 Git是分布式代码托管的标杆,这 ...

  4. C++数组与指针回顾总结

    //数组名是常量指针, //a+1 是相对数组起始地址偏移了sizeof(int)字节 //&a+1 是相对于数组起始地址偏移了sizeof(a)字节 , , , }; cout <&l ...

  5. Vulkan Tutorial 09 图像与视图

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 使用任何的VkImage,包括在交换链或者渲染管线中的,我们都需要创建VkImage ...

  6. Spring Data系列之Jpa(一)

    1.Spring Data的由来 Spring Data是SpringSource基金会创下的一个简化数据库访问.支持云服务的开源框架.其主要目的是让数据库访问变的方便快捷,可以用Spring Dat ...

  7. Logos

    [Logos] Logos is a component of the Theos development suite that allows method hooking code to be wr ...

  8. CSS简单介绍及应用

    CSS的简介 概述: Cascading Style Sheets, 层叠样式表. 作用: 用来美化页面的. 分类: 行内样式: //直接写在元素(html的标签)中的样式. 内部样式: //写在&l ...

  9. 刷题向》图论》BZOJ1179 关于tarjan和SPFA的15秒(normal)

    这道题可以考察图论的掌握程度(算半道水题) 题目如下 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i ...

  10. POI 生成exel报表

    去官网下载相关jar包   http://poi.apache.org/ package poi.zr.com; import java.io.File; import java.io.FileNot ...