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