springboot-注解:@Qualifier

有如下接口:

 
  1. package com.xi.springbootdemo.qualifier;

  2.  
  3. public interface EmployeeService {

  4. public String getEmployeeById(Long id);

  5. }

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

 
  1. package com.xi.springbootdemo.qualifier;

  2.  
  3. import org.springframework.stereotype.Service;

  4.  
  5. @Service("service")

  6. public class EmployeeServiceImpl implements EmployeeService{

  7. @Override

  8. public String getEmployeeById(Long id) {

  9. return "0";

  10. }

  11. }

 
  1. package com.xi.springbootdemo.qualifier;

  2.  
  3. import org.springframework.stereotype.Service;

  4.  
  5. @Service("service1")

  6. public class EmployeeServiceImpl1 implements EmployeeService{

  7. @Override

  8. public String getEmployeeById(Long id) {

  9. return "1";

  10. }

  11. }

调用代码如下:

 
  1. package com.xi.springbootdemo.qualifier;

  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.context.annotation.Conditional;

  5. import org.springframework.stereotype.Controller;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RestController;

  8.  
  9. import javax.servlet.http.HttpServletRequest;

  10. import javax.servlet.http.HttpServletResponse;

  11.  
  12. @RestController

  13. public class EmployeeInfoControl {

  14. @Autowired

  15. private EmployeeService employeeService;

  16.  
  17. @RequestMapping("/emplayee.do")

  18. public void showEmplayeeInfo(){

  19. String employeeById = employeeService.getEmployeeById(1l);

  20. System.out.println("employeeById值为"+employeeById);

  21. }

  22. }

在启动时报如下错误:
 

 
  1. Field employeeService in com.xi.springbootdemo.qualifier.EmployeeInfoControl required a single bean, but 2 were found:

  2. - service: defined in file [F:\DaoEclipseWorkplace\springbootdemo\target\classes\com\xi\springbootdemo\qualifier\EmployeeServiceImpl.class]

  3. - service1: defined in file [F:\DaoEclipseWorkplace\springbootdemo\target\classes\com\xi\springbootdemo\qualifier\EmployeeServiceImpl1.class]

  4.  
  5.  
  6. Action:

  7.  
  8. Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

其实报错信息已经说得很明确了,在autoware时,由于有两个类实现了EmployeeService接口,所以Spring不知道应该绑定哪个实现类,所以抛出了如上错误。

这个时候就要用到@Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称必须为我们之前定义@Service注解的名称之一!

代码如下:

 
  1. package com.xi.springbootdemo.qualifier;

  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.beans.factory.annotation.Qualifier;

  5. import org.springframework.context.annotation.Conditional;

  6. import org.springframework.stereotype.Controller;

  7. import org.springframework.web.bind.annotation.RequestMapping;

  8. import org.springframework.web.bind.annotation.RestController;

  9.  
  10. import javax.servlet.http.HttpServletRequest;

  11. import javax.servlet.http.HttpServletResponse;

  12.  
  13. @RestController

  14. public class EmployeeInfoControl {

  15. @Autowired

  16. @Qualifier("service")

  17. private EmployeeService employeeService;

  18.  
  19. @RequestMapping("/emplayee.do")

  20. public void showEmplayeeInfo(){

  21. String employeeById = employeeService.getEmployeeById(1l);

  22. System.out.println("employeeById值为"+employeeById);

  23. }

  24. }

问题解决,用浏览器访问:

http://localhost:8080/emplayee.do

打印日志如下:

employeeById值为0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值