詳細(xì)解讀spring中的@Resource注解
@Resource屬性介紹
- name:資源的JNDI名稱。在spring的注入時(shí),指定bean的唯一標(biāo)識。
- type:指定bean的類型。
- lookup:引用指向的資源的名稱。它可以使用全局JNDI名稱鏈接到任何兼容的資源。
- authenticationType:指定資源的身份驗(yàn)證類型。它只能為任何受支持類型的連接工廠的資源指定此選項(xiàng),而不能為其他類型的資源指定此選項(xiàng)。
- shareable:指定此資源是否可以在此組件和其他組件之間共享。
- mappedName:指定資源的映射名稱。
- description:指定資源的描述。
@Resource 的裝配規(guī)則
默認(rèn)情況下,即所有屬性都不指定,它默認(rèn)按照byType的方式裝配bean對象。
- 如果指定了name,沒有指定type,則采用byName。
- 如果沒有指定name,而是指定了type,則按照byType裝配bean對象。
- 當(dāng)byName和byType都指定了,兩個(gè)都會校驗(yàn),如果找不到,或者找到多個(gè)(比如byName的方式找到了BeanA, ByType的方式找到了BeanB ) 這種情況也是不會成功的.
上述略顯官方的味道的解釋,相信不少人也是暈暈的 , 也對這個(gè)"默認(rèn)值" 情況解釋的不到位 , 下面來個(gè)靈魂總結(jié).
靈魂總結(jié)
注意: !!! type和name的根本邏輯就是 type是劃定一個(gè)范圍 , 然后name 在其中選擇一個(gè) 舉個(gè)栗子:
- 如果 type 只匹配了 一個(gè) ( 1 ) , 那么成功裝備結(jié)果 必然是 1 , 如果name只有找到了1 , 或者沒有找到的情況下才會配置成功( 沒有顯示指定name值, 默認(rèn)為變量名) , name如果在容器中找到了非1 的bean ,則會報(bào)類型錯(cuò)誤
- 如果type 匹配了 ( 1 , 2 , 3 ) 多個(gè)實(shí)例 , 那么name只有匹配到 其中一個(gè),才會裝配成功, 如果一個(gè)也匹配不到 , 則會報(bào)錯(cuò) ,因?yàn)閟pring不知道到底要裝配哪個(gè)實(shí)例
- 如果type一個(gè)都沒有匹配到,那就直接涼涼了 ,報(bào)錯(cuò):No qualifying bean of type xxx 的錯(cuò)誤, 即使name指定的值在容器中找到了符合條件的bean實(shí)例, 也會報(bào) 類型不符合的錯(cuò)誤
先來看下@Resource的應(yīng)用場景
@Resource的應(yīng)用場景一般都是在裝配的時(shí)候出現(xiàn)了多個(gè)符合條件的bean , 這時(shí)候用@Autowired注解自動裝配就會出現(xiàn)了問題 . 此時(shí)就可以用@Resource注解類解決問題 . (@Qualifier + @Autowired 也可以實(shí)現(xiàn), 這里主要說下@Resource注解) , 通常就是解決多態(tài)的問題.
代碼演示
這里示例將@Resource 放在了類的屬性上
首先有個(gè)HelloService接口:
package com.resource.service;
public interface HelloService {
void sayHello();
}兩個(gè)實(shí)現(xiàn)類 , HelloServiceImpl1 和 HelloServiceImpl2 , 實(shí)現(xiàn)的sayHello()方法,分別在控制臺打印出 hello one! , hello two! 如下:
package com.resource.service.impl;
@Component
public class HelloServiceImpl1 implements HelloService {
public void sayHello() {
System.out.println("hello one!");
}
}package com.resource.service.impl;
@Component
public class HelloServiceImpl2 implements HelloService {
public void sayHello() {
System.out.println("hello two!");
}
}業(yè)務(wù)類UseService 實(shí)現(xiàn)屬性裝配
package com.resource;
@Component
public class UseService {
//@Qualifier("helloServiceImpl1")
//@Autowired
//private HelloService helloService;
@Resource
private HelloService helloServiceImpl;
public void say(){
helloServiceImpl.sayHello();
}
}測試類
public static void main(String[] args) {
//1.創(chuàng)建容器
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("com");
//2.獲取對象
UseService useService = ac.getBean("useService", UseService.class);
useService.say();
}默認(rèn)情況
裝配代碼 其它代碼不變
@Resource
private HelloService helloServiceImpl;
public void say(){
helloServiceImpl.sayHello();
}運(yùn)行測試
這時(shí)候如果什么都不指定, 運(yùn)行則會報(bào)錯(cuò)如下:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.resource.service.HelloService' available: expected single matching bean but found 2:helloServiceImpl1,helloServiceImpl2
分析
@Resource是用 ByType和ByName 來裝配的, 如果沒有顯示的通過type屬性和name屬性指定 , “就會找其默認(rèn)值” 在這個(gè)示例中type就是HelloService.class , 又因?yàn)槠涫墙涌? spring在容器中找到了其兩個(gè)已經(jīng)注入容器的實(shí)現(xiàn)類分別為 helloServiceImpl1 , helloServiceImpl2 這兩個(gè)實(shí)例. (范圍) 而name 也沒有通過屬性執(zhí)行 , name默認(rèn)值就是變量的名稱 helloServiceImpl , 顯然spring容器中是沒有的. (后續(xù)會通過測試驗(yàn)證) 通過默認(rèn)指定值得ByType和ByName 其結(jié)果就是得到了兩個(gè)符合要求的實(shí)例 , 而name也沒有完成在type后有多個(gè)實(shí)例的情況下 “選一個(gè)” . 所有會有上述的把報(bào)錯(cuò)信息.
byName (name默認(rèn)屬性名) 裝配代碼
其它代碼不變
@Resource(type = HelloServiceImpl1.class )
private HelloService helloServiceImpl1;
public void say(){
helloServiceImpl1.sayHello();
}這個(gè)變化就是上個(gè)示例中將屬性名從helloServiceImpl 改成了 helloServiceImpl1 .
運(yùn)行測試
hello one!
Process finished with exit code 0
分析
這里同樣byType后 有兩個(gè)實(shí)例 helloServiceImpl1 , helloServiceImpl2 , 而name沒有顯示指定, 默認(rèn)為變量名 helloServiceImpl1 , 也完成了選一個(gè)的任務(wù), 進(jìn)而裝配成功!
byName (name顯示指定) 裝配代碼
其它代碼不變
@Resource(name="helloServiceImpl2")
private HelloService helloServiceImpl1;
public void say(){
helloServiceImpl1.sayHello();
}這個(gè)變化就是上個(gè)示例中 指定了name屬性的值
運(yùn)行測試
hello two!
Process finished with exit code 0
分析
可以看到屬性名為helloServiceImpl1 , 顯示指定的是helloServiceImpl2 , 即如果顯示指定name的值得話就取該值, 相當(dāng)于是對默認(rèn)的變量名覆蓋了(可以這樣理解). 就是有顯示指定就用顯示指定的, 沒有就用變量名. 結(jié)果輸出hello two! 就是對的了.
byType 顯示指定 裝配代碼
其它代碼不變 , 裝配改為顯示指定type值,如下
@Resource(type = HelloServiceImpl1.class )
private HelloService helloServiceImpl1;
public void say(){
helloServiceImpl1.sayHello();
}這個(gè)變化就是上個(gè)示例中 指定了name屬性的值
運(yùn)行測試
hello one!
Process finished with exit code 0
分析
顯示指定了type = HelloServiceImpl1.class , 也就范圍就是 helloServiceImpl1 , 根據(jù)開頭的靈魂總結(jié) , type已經(jīng)確定了一個(gè) , 那么 name (默認(rèn)變量名 或者顯示指定 ) 的值 就必須是helloServiceImpl1 或者是一個(gè)在spring容器中找不到的名稱.
注意: 這是個(gè)坑, 如果你指定的變量名剛好是spring容器中的某個(gè)bean的id , 那么這里就會報(bào) Bean named ‘xxxx’ is expected to be of type ‘com.resource.service.impl.HelloServiceImpl1’ 的異常!!!
這里用代碼演示下: 新建了個(gè)HiService類
package com.resource.service.impl;
@Component
public class HiService {
public void sayHello() {
System.out.println("Hi Hi!");
}
}裝配類 (主要改了變量名為hiservice ,HiService 的bean id)
@Resource(type = HelloServiceImpl1.class )
private HelloService hiService;
public void say(){
hiService.sayHello();
}運(yùn)行報(bào)類型的錯(cuò)誤如下:
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'hiService' is expected to be of type 'com.resource.service.impl.HelloServiceImpl1'
but was actually of type 'com.resource.service.impl.HiService'
到此這篇關(guān)于詳細(xì)解讀spring中的@Resource注解的文章就介紹到這了,更多相關(guān)spring中的@Resource注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)稀疏矩陣的壓縮與解壓的方法
這篇文章主要介紹了java實(shí)現(xiàn)稀疏矩陣的壓縮與解壓 ,把該稀疏矩陣壓縮以三元組形式表示并以文件形式保存,再寫另一個(gè)程序讀取文件中的信息把壓縮后的三元組還原成原來的稀疏矩陣,需要的朋友可以參考下2022-03-03
Security框架:如何使用CorsFilter解決前端跨域請求問題
這篇文章主要介紹了Security框架:如何使用CorsFilter解決前端跨域請求問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java FileInputStream讀中文亂碼問題解決方案
這篇文章主要介紹了Java FileInputStream讀中文亂碼問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
詳解Java使用雙異步后如何保證數(shù)據(jù)一致性
這篇文章主要為大家詳細(xì)介紹了Java使用雙異步后如何保證數(shù)據(jù)一致性,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以了解下2024-01-01
Java使用hutool工具實(shí)現(xiàn)驗(yàn)證碼登錄
這篇文章主要為大家詳細(xì)介紹了Java如何使用hutool工具實(shí)現(xiàn)驗(yàn)證碼登錄功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
Springboot+echarts實(shí)現(xiàn)可視化
這篇文章主要為大家詳細(xì)介紹了Springboot+echarts實(shí)現(xiàn)可視化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Java中遍歷集合的并發(fā)修改異常解決方案實(shí)例代碼
當(dāng)你遍歷集合的同時(shí),又往集合中添加或者刪除元素,就可能報(bào)并發(fā)修改異常,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷集合的并發(fā)修改異常解決方案的相關(guān)資料,需要的朋友可以參考下2022-12-12

