spring IOC中三種依賴注入方式
一、Spring IOC(依賴注入的三種方式):
1、Setter方法注入。
2、構造方法注入。
使用構造方法,注入bean值。關鍵代碼:public UserServiceImpl(UserDao dao) {this.dao=dao;} <bean class='service.impl.UserServiceImpl'> <constructor-arg><ref bean='dao'/></constructor-arg> </bean>
3、P命名空間注入。
二、Spring IOC(依賴注入的五種不同數據類型):
1、注入直接量(基本數據類型、字符串)
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、使用內部Bean。
<bean class='service.impl.UserServiceImpl'> <property name='dao'> <bean /> </property> </bean>
4、注入集合類型的屬性。
5、注入null和空字符串。
到此這篇關于spring IOC中三種依賴注入方式的文章就介紹到這了,更多相關spring IOC依賴注入內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
