Spring注解實(shí)現(xiàn)自動(dòng)裝配過(guò)程解析
在IOC容器中學(xué)習(xí)相關(guān)注解(常用)
1. @Autowireda.作用對(duì)象:(官網(wǎng)解釋)
1. You can apply the @Autowired annotation to constructors:
2.you can also apply the @Autowired annotation to 'traditional' setter methods:
3.You can also apply the annotation to methods with arbitrary names and/or multiple arguments:
4.You can apply @Autowired to fields as well and even mix it with constructors:
5.It is also possible to provide all beans of a particular type from the ApplicationContext by adding the annotation to a field or method that expects an array of that type:
6.Even typed Maps can be autowired as long as the expected key type is String. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names:等
總結(jié)一下就是: 可以在構(gòu)造器,set方法,任意方法和屬性上,數(shù)組上,String類(lèi)型的Map上等。
Notes:1.@Autowired默認(rèn)按類(lèi)型裝配(這個(gè)注解是屬業(yè)spring的),默認(rèn)情況下必須要求依賴對(duì)象必須存在,如果要允許null值,可以設(shè)置它的required屬性為false。
2.可以與@qualifier 共同使用, 當(dāng)對(duì)象類(lèi)型和名字發(fā)生沖突時(shí),該注解可用于指定特定的對(duì)象。
@Autowired() @Qualifier('cat')
可以找到id='cat'的beanb.功能:它可以對(duì)類(lèi)成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動(dòng)裝配的工作。通過(guò) @Autowired的使用來(lái)消除 set ,get方法。2.@Resourcea.功能: @Resource的作用相當(dāng)于@Autowired,只不過(guò)@Autowired按byType自動(dòng)注入,而@Resource默認(rèn)按 byName自動(dòng)注入
@Resource有兩個(gè)屬性是比較重要的,分是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類(lèi)型。所以如果使用name屬性,則使用byName的自動(dòng)注入策略,而使用type屬性時(shí)則使用byType自動(dòng)注入策略。如果既不指定name也不指定type屬性,這時(shí)將通過(guò)反射機(jī)制使用byName自動(dòng)注入策略。
3.@Requireda.功能:@Required 注釋?xiě)?yīng)用于 bean 屬性的 setter 方法,它表明受影響的 bean 屬性在配置時(shí)必須放在 XML 配置文件中,否則容器就會(huì)拋出一個(gè) BeanInitializationException 異常。下面顯示的是一個(gè)使用 @Required 注釋的示例。
這有一個(gè)很好的解釋和例子關(guān)于@Required注解
使用@Autowired后的優(yōu)點(diǎn)
原來(lái)我們需要手動(dòng)注入之后才可以使用employee對(duì)象:
<bean> <property name='employee' ref='employee'/>
若沒(méi)有進(jìn)行手動(dòng)注入,不會(huì)從測(cè)試代碼中 獲取到employee對(duì)象。
使用@Autowired之后
不需要手動(dòng)注入。
<bean />
只用在屬性上進(jìn)行@Autowired注釋標(biāo)注
在測(cè)試類(lèi)中即可直接調(diào)用:
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext('applicationContext.xml'); Car car = (Car) context.getBean('car'); car.getOwner().MyEmployment(); }}
系統(tǒng)首先根據(jù) bean中class類(lèi)型進(jìn)行確認(rèn),再和bean中id名進(jìn)行確認(rèn),最后確定所定的注入對(duì)象。 若多個(gè)bean 名字不同,且類(lèi)型相同則該注釋失效。(可使用@Qualifier 進(jìn)行唯一指定)
例如:
<bean /> <bean />
運(yùn)行相同代碼會(huì)報(bào)如下錯(cuò)誤:
此時(shí)加上@Qualifier注釋如下,代碼可正常編譯:
運(yùn)行結(jié)果:
如分享內(nèi)容中有問(wèn)題的地方,還望您多加指出,感謝您的瀏覽。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JAMon(Java Application Monitor)備忘記2. 如何用 Python 制作一個(gè)迷宮游戲3. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例4. NetCore 配置Swagger的詳細(xì)代碼5. Java SE 6中JDBC 4.0的增強(qiáng)特性6. idea自定義快捷鍵的方法步驟7. Spring依賴注入的三種方式實(shí)例詳解8. 微信小程序?qū)崿F(xiàn)商品分類(lèi)頁(yè)過(guò)程結(jié)束9. PHP設(shè)計(jì)模式中的命令模式10. 手把手帶你定制.NET 6.0的Middleware中間件
