基于Spring的注解@Qualifier小结

 更新时间:2021年08月27日 10:04:49   作者:小菜的历程  
这篇文章主要介绍了Spring的注解@Qualifier小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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;
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 通过Feign进行调用@FeignClient 找不到的解决方案

    通过Feign进行调用@FeignClient 找不到的解决方案

    这篇文章主要介绍了通过Feign进行调用@FeignClient 找不到的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java源码解析ThreadLocal及使用场景

    Java源码解析ThreadLocal及使用场景

    今天小编就为大家分享一篇关于Java源码解析ThreadLocal及使用场景,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Java8中lambda表达式的应用及一些泛型相关知识

    Java8中lambda表达式的应用及一些泛型相关知识

    这篇文章主要介绍了Java8中lambda表达式的应用及一些泛型相关知识的相关资料
    2017-01-01
  • JPA之QueryDSL-JPA使用指南

    JPA之QueryDSL-JPA使用指南

    Springdata-JPA是对JPA使用的封装,Querydsl-JPA也是基于各种ORM之上的一个通用查询框架,使用它的API类库可以写出Java代码的sql,下面就来介绍一下JPA之QueryDSL-JPA使用指南
    2023-11-11
  • SpringBoot整合SpringSecurity和JWT和Redis实现统一鉴权认证

    SpringBoot整合SpringSecurity和JWT和Redis实现统一鉴权认证

    Spring Security是一个可以为Java应用程序提供全面安全服务的框架,同时它也可以轻松扩展以满足自定义需求,本文主要介绍了SpringBoot整合SpringSecurity和JWT和Redis实现统一鉴权认证,感兴趣的可以了解一下
    2023-11-11
  • java代码mqtt接收发送消息方式

    java代码mqtt接收发送消息方式

    这篇文章主要介绍了java代码mqtt接收发送消息方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • 详解Java集合中的基本数据结构

    详解Java集合中的基本数据结构

    总有小伙伴让我总结一下Java集合中的基本数据结构的相关知识,今天特地整理了本篇文章,文中有非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • springboot配置项目启动后自动打开浏览器访问项目方式

    springboot配置项目启动后自动打开浏览器访问项目方式

    这篇文章主要介绍了springboot配置项目启动后自动打开浏览器访问项目方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Eclipse新建项目不可选择Java Project问题解决方案

    Eclipse新建项目不可选择Java Project问题解决方案

    这篇文章主要介绍了Eclipse新建项目不可选择Java Project问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Springboot AOP开发教程

    Springboot AOP开发教程

    AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型,本文给大家介绍Springboot AOP开发教程,感兴趣的朋友跟随小编一起看看吧
    2024-03-03

最新评论