Spring的注解@Qualifier小结
来源:互联网 发布:sap供应商主数据导出 编辑:程序博客网 时间:2024/05/21 10:08
Spring的注解@Qualifier小结
近期在捯饬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) { #略 }}
问题解决!
0 0
- Spring的注解@Qualifier小结
- Spring的注解@Qualifier小结
- Spring的注解@Qualifier小结
- Spring的注解@Qualifier小结
- Spring的@Qualifier注解
- Spring的注解@Qualifier
- Spring的注解@Qualifier小结:一个接口有两个实现类,spring注入时报错
- spring的注解使用,Autowired和Qualifier
- spring框架下的 @Qualifier注解
- Spring的注解@Qualifier小总结
- Spring的@Autowired注解、@Qualifier注解、@Resource注解
- spring @Qualifier注解
- spring @Qualifier注解
- spring @Qualifier注解
- spring @Qualifier注解使用
- spring @Qualifier 注解
- spring @Qualifier注解
- spring中的@Qualifier注解
- 用于两线制4-20mA电路24V电源输出的电流保护电路
- 关于list转换json
- ubuntu16.04安装ncurses-devel
- HDU 1512 Monkey King 可并堆,左偏树
- Inversion
- Spring的注解@Qualifier小结
- Android Kernel下载编译
- iOS即时通讯
- 洛谷P1012 拼数
- 【双11背后的技术】基于深度强化学习与自适应在线学习的搜索和推荐算法研究
- 针对有些sdk只提供了32位so库问题的处理
- Java 数组、List、Set、Map相互转换
- 2016年RN
- Win7安装Redis