spring IOC中三種依賴注入方式
一、Spring IOC(依賴注入的三種方式):
1、Setter方法注入。
2、構(gòu)造方法注入。
使用構(gòu)造方法,注入bean值。關(guān)鍵代碼:public UserServiceImpl(UserDao dao) {this.dao=dao;} <bean class='service.impl.UserServiceImpl'> <constructor-arg><ref bean='dao'/></constructor-arg> </bean>
3、P命名空間注入。
二、Spring IOC(依賴注入的五種不同數(shù)據(jù)類型):
1、注入直接量(基本數(shù)據(jù)類型、字符串)
2、引用其他Bean組件。(面向接口編程)
ref屬性:
<bean class='dao.impl.UserDaoImpl'></bean><bean class='service.impl.UserServiceImpl'> <property name='dao' ref='dao'></property></bean>
<ref>子元素:
<bean class='dao.impl.UserDaoImpl'></bean><bean class='service.impl.UserServiceImpl'> <property name='dao'> <ref bean='dao'/> </property></bean>
p命名空間:
xmlns:p='http://www.springframework.org/schema/p'<bean class='dao.impl.UserDaoImpl'></bean><bean p:dao-ref='dao'></bean>
3、使用內(nèi)部Bean。
<bean class='service.impl.UserServiceImpl'> <property name='dao'> <bean /> </property> </bean>
4、注入集合類型的屬性。
5、注入null和空字符串。
到此這篇關(guān)于spring IOC中三種依賴注入方式的文章就介紹到這了,更多相關(guān)spring IOC依賴注入內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 理解PHP5中static和const關(guān)鍵字2. Android table布局開發(fā)實(shí)現(xiàn)簡單計(jì)算器3. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼4. IntelliJ IDEA安裝插件的方法步驟5. php模擬實(shí)現(xiàn)斗地主發(fā)牌6. vue 使用localstorage實(shí)現(xiàn)面包屑的操作7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Python random庫使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備