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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. html小技巧之td,div標(biāo)簽里內(nèi)容不換行2. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁3. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))4. js的一些潛在規(guī)則使用分析5. JSP之表單提交get和post的區(qū)別詳解及實(shí)例6. ASP實(shí)現(xiàn)加法驗(yàn)證碼7. 詳解瀏覽器的緩存機(jī)制8. XHTML 1.0:標(biāo)記新的開端9. asp讀取xml文件和記數(shù)10. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
