您的位置:首页技术文章
文章详情页

spring IOC中三种依赖注入方式

浏览:67日期:2023-12-02 18:25:16

一、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依赖注入内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: