Spring IOC (DI) 依賴注入的四種方式示例詳解
依賴注入的四種方式:
set 注入
賦值,默認使用的是set() 方法,依賴注入底層是通過反射實現(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ù)順序相同的話,可以不寫index <constructor-arg value=""></constructor-arg> 參數(shù)順序不同的話,寫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”
也可以這樣引入,它會自動引入命名空間
引入命名空間后,就可以這樣以這樣的方式實現(xiàn)依賴注入
<bean id="student" class="cust.zaw.entity.Student" p:stuAge="" p:stuName="" p:stuNO-ref=""> </bean>
- 注意:
無論是String 還是int / short / long ,在賦值時都是value=“值”,因此建議 配合type / name 進行區(qū)分
- IOC 容器賦值:
簡單類型,value=“”對象類型(當一個類中有其他類 類型,為其賦值時使用),ref=“需要引用的id值”
注入各種集合數(shù)據(jù)類型
List Set Map properties
寫一個實體類
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; } }
編寫配置文件
<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); ?? ?}
給對象類型賦值null:
<!-- 注意沒有<value> --> <property name="name"> <null/> </property>
賦空值
<property name="name"> <value></value> </property>
自動裝配注入(只適用于 ref 類型):
約定優(yōu)于配置
- byName:自動尋找:其他bean的id值 = 該Course類的屬性名
- byType:其他類型(class)是否與該Course類的ref 屬性類型一致
- (必須滿足當前ioc容器只有一個bean滿足)
- constructor:其他bean的類型(class)是否與該Course 類的構(gòu)造方法參數(shù) 的類型一致;
<!-- autowire="byName" Course類中有一個ref屬性teacher(屬性名),并且該ioc容器中恰好有一個 bean的id也是teacher bean的id=類的屬性名 --> <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è)置成自動裝配自動裝配雖然可以減少代碼量,但是會降低程序可讀性
<beans xmlns="http://www.springframework.org/schema/beans" .... default-autowire="byName">
到此這篇關(guān)于Spring IOC (DI) 依賴注入的四種方式的文章就介紹到這了,更多相關(guān)Spring IOC依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring:@Async注解和AsyncResult與CompletableFuture使用問題
這篇文章主要介紹了Spring:@Async注解和AsyncResult與CompletableFuture使用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08