Spring IOC (DI) 依賴(lài)注入的四種方式示例詳解
依賴(lài)注入的四種方式:
set 注入
賦值,默認(rèn)使用的是set() 方法,依賴(lài)注入底層是通過(guò)反射實(shí)現(xiàn)的
<bean id="student" class="cust.zaw.entity.Student"> <property name="stuName" value="lc"></property> </bean>
構(gòu)造器注入
使用構(gòu)造方法注入
<bean id="student" class="cust.zaw.entity.Student"> 參數(shù)順序相同的話,可以不寫(xiě)index <constructor-arg value=""></constructor-arg> 參數(shù)順序不同的話,寫(xiě)index或者name或者type <constructor-arg value="" index=“0”></constructor-arg> <constructor-arg value="" index=“1”></constructor-arg> <constructor-arg value="" name=“”></constructor-arg> <constructor-arg value="" type=“”></constructor-arg> </bean>
p命名空間注入
引入命名空間就是加一句話:xmlns:p=“http://www.springframework.org/schema/p”
也可以這樣引入,它會(huì)自動(dòng)引入命名空間
引入命名空間后,就可以這樣以這樣的方式實(shí)現(xiàn)依賴(lài)注入
<bean id="student" class="cust.zaw.entity.Student" p:stuAge="" p:stuName="" p:stuNO-ref=""> </bean>
- 注意:
無(wú)論是String 還是int / short / long ,在賦值時(shí)都是value=“值”,因此建議 配合type / name 進(jìn)行區(qū)分
- IOC 容器賦值:
簡(jiǎn)單類(lèi)型,value=“”對(duì)象類(lèi)型(當(dāng)一個(gè)類(lèi)中有其他類(lèi) 類(lèi)型,為其賦值時(shí)使用),ref=“需要引用的id值”
注入各種集合數(shù)據(jù)類(lèi)型
List Set Map properties
寫(xiě)一個(gè)實(shí)體類(lèi)
package cust.zaw.entity; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class AllCollectionType { private List<String> list; private String[] array; private Set<String> set; private Map<String,String> map; private Properties props; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProps() { return props; } public void setProps(Properties props) { this.props = props; } @Override public String toString() { String strContent=null; for(String str : array) { strContent +=str + ","; } return "AllCollectionType [getList()=" + getList() + ", getArray()=" + Arrays.toString(getArray()) + ", getSet()=" + getSet() + ", getMap()=" + getMap() + ", getProps()=" + getProps() + "]"+strContent; } }
編寫(xiě)配置文件
<bean id="collectionDemo" class="cust.zaw.entity.AllCollectionType"> <property name="list"> <list> <value>足球</value> <value>籃球</value> <value>乒乓球</value> </list> </property> <property name="array"> <array> <value>足球1</value> <value>籃球1</value> <value>乒乓球1</value> </array> </property> <property name="set"> <set> <value>足球2</value> <value>籃球2</value> <value>乒乓球2</value> </set> </property> <property name="map"> <map> <entry> <key> <value>football3</value> </key> <value>足球3</value> </entry> <entry> <key> <value>basketball3</value> </key> <value>籃球3</value> </entry> <entry> <key> <value>ppq3</value> </key> <value>乒乓球3</value> </entry> </map> </property> <property name="props"> <props> <prop key="football4">足球4</prop> <prop key="baskball4">籃球4</prop> <prop key="pp4">乒乓球4</prop> </props> </property> </bean>
獲取輸出
public static void collectionDemp() { ?? ??? ?ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); ?? ??? ?AllCollectionType type=(AllCollectionType)context.getBean("collectionDemo"); ?? ??? ?System.out.println(type); ?? ?}
給對(duì)象類(lèi)型賦值null:
<!-- 注意沒(méi)有<value> --> <property name="name"> <null/> </property>
賦空值
<property name="name"> <value></value> </property>
自動(dòng)裝配注入(只適用于 ref 類(lèi)型):
約定優(yōu)于配置
- byName:自動(dòng)尋找:其他bean的id值 = 該Course類(lèi)的屬性名
- byType:其他類(lèi)型(class)是否與該Course類(lèi)的ref 屬性類(lèi)型一致
- (必須滿足當(dāng)前ioc容器只有一個(gè)bean滿足)
- constructor:其他bean的類(lèi)型(class)是否與該Course 類(lèi)的構(gòu)造方法參數(shù) 的類(lèi)型一致;
<!-- autowire="byName" Course類(lèi)中有一個(gè)ref屬性teacher(屬性名),并且該ioc容器中恰好有一個(gè) bean的id也是teacher bean的id=類(lèi)的屬性名 --> <bean id="student" class="cust.zaw.entity.Student" autowire="byName"> <property name="stuNO" value="2"></property> <property name="stuName" value="lc"></property> <property name="stuAge" value="24"></property> <!-- <property name="teacher" ref="teacher"></property>--> </bean>
可以在頭文件中,一次性將ioc容器的所有bean 統(tǒng)一設(shè)置成自動(dòng)裝配自動(dòng)裝配雖然可以減少代碼量,但是會(huì)降低程序可讀性
<beans xmlns="http://www.springframework.org/schema/beans" .... default-autowire="byName">
到此這篇關(guān)于Spring IOC (DI) 依賴(lài)注入的四種方式的文章就介紹到這了,更多相關(guān)Spring IOC依賴(lài)注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring:@Async注解和AsyncResult與CompletableFuture使用問(wèn)題
這篇文章主要介紹了Spring:@Async注解和AsyncResult與CompletableFuture使用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Java類(lèi)的定義以及執(zhí)行順序?qū)W習(xí)教程
這篇文章主要介紹了Java類(lèi)的定義以及執(zhí)行順序?qū)W習(xí)教程,包括對(duì)象的創(chuàng)建等面向?qū)ο缶幊痰幕A(chǔ)知識(shí),需要的朋友可以參考下2015-09-09MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析
這篇文章主要介紹了MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08通過(guò)Spring層面進(jìn)行事務(wù)回滾的實(shí)現(xiàn)
本文主要介紹了通過(guò)Spring層面進(jìn)行事務(wù)回滾的實(shí)現(xiàn),包括聲明式事務(wù)和編程式事務(wù),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04在Mybatis中association標(biāo)簽多層嵌套的問(wèn)題
這篇文章主要介紹了在Mybatis中association標(biāo)簽多層嵌套的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03