基于Spring的注解@Qualifier小結(jié)
Spring的注解@Qualifier小結(jié)
近期在捯飭spring的注解,現(xiàn)將遇到的問題記錄下來,以供遇到同樣問題的童鞋解決~
先說明下場景,代碼如下
有如下接口:
public interface EmployeeService {
public EmployeeDto getEmployeeById(Long id);
}
同時有下述兩個實現(xiàn)類 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();
}
}
調(diào)用代碼如下:
@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]
其實報錯信息已經(jīng)說得很明確了,在autoware時,由于有兩個類實現(xiàn)了EmployeeService接口,所以Spring不知道應(yīng)該綁定哪個實現(xiàn)類,所以拋出了如上錯誤。
這個時候就要用到@Qualifier注解了,qualifier的意思是合格者,通過這個標示,表明了哪個實現(xiàn)類才是我們所需要的,我們修改調(diào)用代碼,添加@Qualifier注解,需要注意的是@Qualifier的參數(shù)名稱必須為我們之前定義@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應(yīng)該被自動注入。當(dāng)Spring無法判斷出哪個bean應(yīng)該被注入時,@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應(yīng)該自動注入?不。當(dāng)您運行上面的例子時,拋出如下異常:
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應(yīng)該被autowired的。
public class Staff
{
@Autowired
@Qualifier("user1")
private User user;
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis SELECT基本查詢實現(xiàn)方法詳解
這篇文章主要介紹了MyBatis SELECT基本查詢實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
Spring?session?redis?修改默認的序列化方法(案例)
這篇文章主要介紹了Spring?session?redis?修改默認的序列化方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
Java通過Freemarker模板實現(xiàn)生成Word文件
FreeMarker是一款模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本的通用工具。本文將根據(jù)Freemarker模板實現(xiàn)生成Word文件,需要的可以參考一下2022-09-09
Java深入了解數(shù)據(jù)結(jié)構(gòu)之哈希表篇
哈希表是一種根據(jù)關(guān)鍵碼去尋找值的數(shù)據(jù)映射結(jié)構(gòu),該結(jié)構(gòu)通過把關(guān)鍵碼映射的位置去尋找存放值的地方,說起來可能感覺有點復(fù)雜,我想我舉個例子你就會明白了,最典型的的例子就是字典2022-01-01
mybatis實現(xiàn)一對一關(guān)聯(lián)映射實例代碼
這篇文章主要給大家介紹了關(guān)于mybatis實現(xiàn)一對一關(guān)聯(lián)映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

