基于Spring的注解@Qualifier小结
作者:小菜的历程 时间:2022-12-20 23:17:50
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) {
#略
}
}
问题解决!
@qualifier注解
@Qualifier限定哪个bean应该被自动注入。当Spring无法判断出哪个bean应该被注入时,@Qualifier注解有助于消除歧义bean的自动注入。
参见下面的例子
public class Staff{
@Autowired
private user user;
}
我们有两个bean定义为Person类的实例。
<beanid="staff"class="com.test.Staff"/>
<beanid="user1"class="com.test.User">
<property name="name"value="zhangsan"/></bean>
<beanid="user2"class="com.test.User">
<property name="name"value="lisi"/></bean>
Spring 知道哪个bean应该自动注入?不。当您运行上面的例子时,抛出如下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.test.User] is defined: expected single matching bean but found2: [user1, user2]
要解决以上问题,你需要使用@Quanlifier注解告诉Spring 哪个bean应该被autowired的。
public class Staff
{
@Autowired
@Qualifier("user1")
private User user;
}
来源:https://www.cnblogs.com/smileLuckBoy/p/5801678.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot Logback配置日志过程解析
Flutter加载图片流程之ImageProvider源码示例解析
C#实现最完整的文件和目录操作类实例
Lombok基本注解之@SneakyThrows的作用
![](https://img.aspxhome.com/file/2023/0/77340_0s.png)
浅谈web服务器项目中静态请求和动态请求处理
![](https://img.aspxhome.com/file/2023/6/89466_0s.png)
java返回集合为null还是空集合及空集合的三种写法小结
详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)
![](https://img.aspxhome.com/file/2023/0/58690_0s.png)
基于WPF实现用户头像选择器的示例代码
SpringBoot下的值注入(推荐)
C# 字符串的连接(实例讲解)
![](https://img.aspxhome.com/file/2023/0/104210_0s.png)
Android自定义View实现钟摆效果进度条PendulumView
![](https://img.aspxhome.com/file/2023/1/126751_0s.gif)
Android App中使用ViewPager+Fragment实现滑动切换效果
![](https://img.aspxhome.com/file/2023/3/137203_0s.jpg)
Android提高之MediaPlayer播放网络音频的实现方法
![](https://img.aspxhome.com/file/2023/1/102601_0s.png)
spring入门教程之bean的继承与自动装配详解
Java编程实现对十六进制字符串异或运算代码示例
![](https://img.aspxhome.com/file/2023/2/58892_0s.png)
C# 特性AttributeUsage简介与使用教程
![](https://img.aspxhome.com/file/2023/1/111141_0s.png)
Iconfont(矢量图标)+iconmoon(图标svg互转)配合javascript实现社交分享系统
![](https://img.aspxhome.com/file/2023/0/64290_0s.png)
Android 5.0+ 屏幕录制实现的示例代码
![](https://img.aspxhome.com/file/2023/7/116007_0s.png)
解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
Android studio设计简易计算器
![](https://img.aspxhome.com/file/2023/2/138282_0s.jpg)