我是靠谱客的博主 唠叨石头,这篇文章主要介绍Spring DI依赖注入,现在分享给大家,希望可以做个参考。

一、两种注入方式介绍

  DI(Dependency Injection),依赖注入,即组件之间的依赖关系由容器在应用系统运行期来决定,也就是由容器动态地将某种依赖关系的目标对象实例注入到应用系统中的各个关联的组件之中。简单来说,所谓的依赖注入其实就是,在创建对象的同时或之后,如何给对象的属性赋值。如果对象由我们自己创建,这一切都变得很简单,例如:

复制代码
1
2
3
4
User user = new User(); user.setName("zs"); user.setAge(18)

或者:

复制代码
1
2
User user = new User("zs", 18);

如果对象由spring创建,那么spring是怎么给属性赋值的?spring提供两种方式为属性赋值:
(1).Set方式注入
(2).构造方法注入(spring大都使用这种方式)

二、set方式注入

1.普通属性注入

案例:通过Spring创建User实例,并为User实例的name和age属性(普通属性)赋值
1)创建User类,声明name和age属性,并添加对应的setter和getter方法,以及toString方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.company.spring; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }

2)在applicationContext.xml中声明User类的bean实例

复制代码
1
2
3
<!-- 声明User类的bean实例 --> <bean id="user" class="com.tedu.spring.User"></bean>

3)创建测试类—TestDI

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.company.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDI { public static void main(String[] args) { //1.加载applicationContext.xml核心配置文件 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml" ); //2.获取User类的实例 User user = (User) ac.getBean("user"); //3.输出User实例 System.out.println( user ); } }

4)修改applicationContext.xml中User实例的声明,为User实例注入属性

复制代码
1
2
3
4
5
6
7
<!-- 声明User类的bean实例 --> <bean id="user" class="com.company.spring.User"> <!-- 通过set方式为普通属性赋值 --> <property name="name" value="zs"></property> <property name="age" value="20"></property> </bean>

其中name属性的值,必须要和User类中所注入属性对应的get方法的名字去掉get后首字母变为小写的名字相同。
5)运行测试类TestDI

2.对象属性注入

1)创建UserInfo类

复制代码
1
2
3
4
5
package com.company.spring; public class UserInfo { }

2)在applicationContext.xml中,声明UserInfo类的bean实例

复制代码
1
2
3
<!-- 声明UserInfo类的bean实例 --> <bean id="userInfo" class="com.company.spring.UserInfo"></bean>

3)修改User类,声明userInfo属性,添加对应的setter和getter方法,并重新生成toString方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class User { ... private UserInfo userInfo; public UserInfo getUserInfo() { return userInfo; } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } ... public String toString() { return "User [name=" + name + ", age=" + age + ", userInfo=" + userInfo + "]"; } }

4)在applicationContext.xml中,将UserInfo对象作为值,赋值给User对象的userInfo属性

复制代码
1
2
3
4
5
6
7
8
9
<!-- 声明User类的bean实例 --> <bean id="user" class="com.tedu.spring.User"> <!-- 通过set方式为普通属性赋值 --> <property name="name" value="韩少云"></property> <property name="age" value="20"></property> <!-- 通过set方式为对象属性赋值 --> <property name="userInfo" ref="userInfo"></property> </bean>

由于此处是将UserInfo对象作为值赋值给另一个对象的属性,因此ref属性的值,为UserInfo对象bean标签的id值。对象属性通过ref属性注入。
5)运行测试类TestDI

三、构造方法注入

案例:通过Spring创建User对象,并为User对象的属性(name、age、UserInfo属性)赋值

1)为User类声明构造函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
//声明无参构造函数 public User() { } //声明有参构造函数 public User(String name, Integer age, UserInfo userInfo) { super(); this.name = name; this.age = age; this.userInfo = userInfo; }

2)修改applicationContext.xml文件,将set方式修改为构造方法注入。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<bean id="user" class="com.company.spring.User"> <!-- 通过set方式为普通属性赋值 <property name="name" value="zs"></property> <property name="age" value="20"></property> <property name="userInfo" ref="userInfo"></property> --> <!-- 通过构造器中参数为属性赋值 --> <constructor-arg name="name" value="马云"></constructor-arg> <constructor-arg name="age" value="35"></constructor-arg> <constructor-arg name="userInfo" ref="userInfo"></constructor-arg> </bean> <!-- 声明UserInfo类的bean实例 --> <bean id="userInfo" class="com.company.spring.UserInfo"></bean>

其中,constructor-arg标签name属性的值必须和构造函数中参数的名字相同.同样的,普通属性直接通过value注入即可;对象属性通过ref属性注入。
3)运行测试类TestDI

最后

以上就是唠叨石头最近收集整理的关于Spring DI依赖注入的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部