@Resource详解-代码示例
@Resource注解詳解
- 屬性介紹
- @Resource 的裝配規(guī)則
- 靈魂總結(jié)
- 先來(lái)看下@Resource的應(yīng)用場(chǎng)景
- 代碼演示
- 默認(rèn)情況
- 裝配代碼
- 運(yùn)行測(cè)試
- 分析
- byName (name默認(rèn)屬性名)
- 裝配代碼
- 運(yùn)行測(cè)試
- 分析
- byName (name顯示指定)
- 裝配代碼
- 運(yùn)行測(cè)試
- 分析
- byType 顯示指定
- 裝配代碼
- 運(yùn)行測(cè)試
- 分析
- finally
此注解來(lái)源于JSR規(guī)范(Java Specification Requests),其作用是找到依賴的組件注入到應(yīng)用來(lái),它利用了JNDI(Java Naming and Directory Interface Java命名目錄接口 J2EE規(guī)范之一)技術(shù)查找所需的資源。
網(wǎng)上查了些資料看的有點(diǎn)暈暈, 這里用例子來(lái)說(shuō)明 @Resource的用法 , 以及需要注意的問(wèn)題
屬性介紹
name:資源的JNDI名稱。在spring的注入時(shí),指定bean的唯一標(biāo)識(shí)。 type:指定bean的類型。 lookup:引用指向的資源的名稱。它可以使用全局JNDI名稱鏈接到任何兼容的資源。 authenticationType:指定資源的身份驗(yàn)證類型。它只能為任何受支持類型的連接工廠的資源指定此選項(xiàng),而不能為其他類型的資源指定此選項(xiàng)。 shareable:指定此資源是否可以在此組件和其他組件之間共享。 mappedName:指定資源的映射名稱。 description:指定資源的描述。@Resource 的裝配規(guī)則
默認(rèn)情況下,即所有屬性都不指定,它默認(rèn)按照byType的方式裝配bean對(duì)象。
如果指定了name,沒(méi)有指定type,則采用byName。
如果沒(méi)有指定name,而是指定了type,則按照byType裝配bean對(duì)象。
當(dāng)byName和byType都指定了,兩個(gè)都會(huì)校驗(yàn),如果找不到,或者找到多個(gè)(比如byName的方式找到了BeanA, ByType的方式找到了BeanB ) 這種情況也是不會(huì)成功的.
上述略顯官方的味道的解釋,相信不少人也是暈暈的 , 也對(duì)這個(gè)"默認(rèn)值" 情況解釋的不到位 , 下面來(lái)個(gè)靈魂總結(jié).
靈魂總結(jié)
注意: !!! type和name的根本邏輯就是 type是劃定一個(gè)范圍 , 然后name 在其中選擇一個(gè)
舉個(gè)栗子:
- 如果 type 只匹配了 一個(gè) ( 1 ) , 那么成功裝備結(jié)果 必然是 1 , 如果name只有找到了1 , 或者沒(méi)有找到的情況下才會(huì)配置成功( 沒(méi)有顯示指定name值, 默認(rèn)為變量名) , name如果在容器中找到了非1 的bean ,則會(huì)報(bào)類型錯(cuò)誤.
- 如果type 匹配了 ( 1 , 2 , 3 ) 多個(gè)實(shí)例 , 那么name只有匹配到 其中一個(gè),才會(huì)裝配成功, 如果一個(gè)也匹配不到 , 則會(huì)報(bào)錯(cuò) ,因?yàn)閟pring不知道到底要裝配哪個(gè)實(shí)例.
- 如果type一個(gè)都沒(méi)有匹配到,那就直接涼涼了 ,報(bào)錯(cuò):No qualifying bean of type xxx 的錯(cuò)誤, 即使name指定的值在容器中找到了符合條件的bean實(shí)例, 也會(huì)報(bào) 類型不符合的錯(cuò)誤.
先來(lái)看下@Resource的應(yīng)用場(chǎng)景
@Resource的應(yīng)用場(chǎng)景一般都是在裝配的時(shí)候出現(xiàn)了多個(gè)符合條件的bean , 這時(shí)候用@Autowired注解自動(dòng)裝配就會(huì)出現(xiàn)了問(wèn)題 . 此時(shí)就可以用@Resource注解類解決問(wèn)題 . (@Qualifier + @Autowired 也可以實(shí)現(xiàn), 這里主要說(shuō)下@Resource注解) , 通常就是解決多態(tài)的問(wèn)題.
代碼演示
這里示例將@Resource 放在了類的屬性上
首先有個(gè)HelloService接口:
package com.resource.service; public interface HelloService {void sayHello(); }兩個(gè)實(shí)現(xiàn)類 , HelloServiceImpl1 和 HelloServiceImpl2 , 實(shí)現(xiàn)的sayHello()方法,分別在控制臺(tái)打印出 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;@Resourceprivate HelloService helloServiceImpl;public void say(){helloServiceImpl.sayHello();} }測(cè)試類
public static void main(String[] args) {//1.創(chuàng)建容器AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("com");//2.獲取對(duì)象UseService useService = ac.getBean("useService", UseService.class);useService.say();}默認(rèn)情況
裝配代碼
其它代碼不變
@Resourceprivate HelloService helloServiceImpl;public void say(){helloServiceImpl.sayHello();}運(yùn)行測(cè)試
這時(shí)候如果什么都不指定, 運(yùn)行則會(huì)報(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 來(lái)裝配的, 如果沒(méi)有顯示的通過(guò)type屬性和name屬性指定 , “就會(huì)找其默認(rèn)值”
在這個(gè)示例中type就是HelloService.class , 又因?yàn)槠涫墙涌? spring在容器中找到了其兩個(gè)已經(jīng)注入容器的實(shí)現(xiàn)類分別為 helloServiceImpl1 , helloServiceImpl2 這兩個(gè)實(shí)例. (范圍)
而name 也沒(méi)有通過(guò)屬性執(zhí)行 , name默認(rèn)值就是變量的名稱 helloServiceImpl , 顯然spring容器中是沒(méi)有的. (后續(xù)會(huì)通過(guò)測(cè)試驗(yàn)證)
通過(guò)默認(rèn)指定值得ByType和ByName 其結(jié)果就是得到了兩個(gè)符合要求的實(shí)例 , 而name也沒(méi)有完成在type后有多個(gè)實(shí)例的情況下 “選一個(gè)” . 所有會(huì)有上述的把報(bào)錯(cuò)信息.
byName (name默認(rèn)屬性名)
裝配代碼
其它代碼不變
@Resourceprivate HelloService helloServiceImpl1;public void say(){helloServiceImpl1.sayHello();}這個(gè)變化就是上個(gè)示例中將屬性名從helloServiceImpl 改成了 helloServiceImpl1 .
運(yùn)行測(cè)試
hello one! Process finished with exit code 0分析
這里同樣byType后 有兩個(gè)實(shí)例 helloServiceImpl1 , helloServiceImpl2 , 而name沒(méi)有顯示指定, 默認(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)行測(cè)試
hello two! Process finished with exit code 0分析
可以看到屬性名為helloServiceImpl1 , 顯示指定的是helloServiceImpl2 , 即如果顯示指定name的值得話就取該值, 相當(dāng)于是對(duì)默認(rèn)的變量名覆蓋了(可以這樣理解). 就是有顯示指定就用顯示指定的, 沒(méi)有就用變量名. 結(jié)果輸出hello two! 就是對(duì)的了.
byType 顯示指定
裝配代碼
其它代碼不變 , 裝配改為顯示指定type值,如下
@Resource(type = HelloServiceImpl1.class )private HelloService helloServiceImpl1;public void say(){helloServiceImpl1.sayHello();}這個(gè)變化就是上個(gè)示例中 指定了name屬性的值
運(yùn)行測(cè)試
hello one! Process finished with exit code 0分析
顯示指定了type = HelloServiceImpl1.class , 也就范圍就是 helloServiceImpl1 , 根據(jù)開(kāi)頭的靈魂總結(jié) , type已經(jīng)確定了一個(gè) , 那么 name (默認(rèn)變量名 或者顯示指定 ) 的值 就必須是helloServiceImpl1 或者是一個(gè)在spring容器中找不到的名稱.
注意: 這是個(gè)坑, 如果你指定的變量名剛好是spring容器中的某個(gè)bean的id , 那么這里就會(huì)報(bào)
Bean named ‘xxxx’ is expected to be of type ‘com.resource.service.impl.HelloServiceImpl1’ 的異常!!!
這里用代碼演示下:
新建了個(gè)HiService類
裝配類 (主要改了變量名為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'finally
以上僅為個(gè)人見(jiàn)解 , 能看到這里希望能讓您有所收獲.
拋磚引玉 , 如有不到之處, 敬請(qǐng)指正.
總結(jié)
以上是生活随笔為你收集整理的@Resource详解-代码示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Win11如何查看硬盘型号?
- 下一篇: Angular ng命令