Spring的注解@Qualifier用法,以及什么时候用?

本章讲一下@Qualifier注解的用法。以及报的一个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeInfoControl': Injection of autowired dependencies failed;

那么要想知道@Qualifier注解怎么用 。先看下面的场景!认真看完你就懂了。

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

有如下接口:

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

 

调用代码如下:

@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) {
        #略
    }
}

 

 问题解决! 2018-03-28 16:27:56

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:HONGLINCHEN.上海·黄埔

作者邮箱:xychenhonglin@163.com

请关注作者的今日头条号:《专注JavaWeb开发》 博客地址:

请关注作者的CSDN博客:《专注JavaWeb开发》 博客地址:

作者的QQ群:专注JavaWeb开发.群号:162582394



@Autowired @Resource @Qualifier的区别

实用理解:@Autowired @Resource 二选其一,看中哪个就用哪个。

简单理解:

@Autowired 根据类型注入, 

@Resource 默认根据名字注入,其次按照类型搜索

@Autowired @Qualifie("userService") 两个结合起来可以根据名字和类型注入


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页