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)文章:
