近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~
先说明下场景,代码如下:
有如下接口:
package com.xi.springbootdemo.qualifier;
public interface EmployeeService {
public String getEmployeeById(Long id);
}
同时有下述两个实现类 EmployeeServiceImpl和EmployeeServiceImpl1:
package com.xi.springbootdemo.qualifier;
import org.springframework.stereotype.Service;
@Service("service")
public class EmployeeServiceImpl implements EmployeeService{
@Override
public String getEmployeeById(Long id) {
return "0";
}
}
package com.xi.springbootdemo.qualifier;
import org.springframework.stereotype.Service;
@Service("service1")
public class EmployeeServiceImpl1 implements EmployeeService{
@Override
public String getEmployeeById(Long id) {
return "1";
}
}
调用代码如下:
package com.xi.springbootdemo.qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
public class EmployeeInfoControl {
@Autowired
private EmployeeService employeeService;
@RequestMapping("/emplayee.do")
public void showEmplayeeInfo(){
String employeeById = employeeService.getEmployeeById(1l);
System.out.println("employeeById值为"+employeeById);
}
}
在启动时报如下错误:
Field employeeService in com.xi.springbootdemo.qualifier.EmployeeInfoControl required a single bean, but 2 were found:
- service: defined in file [F:\DaoEclipseWorkplace\springbootdemo\target\classes\com\xi\springbootdemo\qualifier\EmployeeServiceImpl.class]
- service1: defined in file [F:\DaoEclipseWorkplace\springbootdemo\target\classes\com\xi\springbootdemo\qualifier\EmployeeServiceImpl1.class]
Action:
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注解的名称之一!
代码如下:
package com.xi.springbootdemo.qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
public class EmployeeInfoControl {
@Autowired
@Qualifier("service")
private EmployeeService employeeService;
@RequestMapping("/emplayee.do")
public void showEmplayeeInfo(){
String employeeById = employeeService.getEmployeeById(1l);
System.out.println("employeeById值为"+employeeById);
}
}
问题解决,用浏览器访问:
http://localhost:8080/emplayee.do
打印日志如下:
employeeById值为0