Spring-(五)不同数据类型的依赖注入

本文最后更新于:January 28, 2022 pm

Spring 是目前主流的 Java Web 开发框架,是 Java 世界最为成功的框架。Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。该框架是一个轻量级的开源框架,具有很高的凝聚力和吸引力。Spring 框架不局限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 框架还是一个超级粘合平台,除了自己提供功能外,还提供粘合其他技术和框架的能力。

目录

环境准备

依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>

Address.java 类

1
2
3
4
5
6
7
8
9
10
11
package com.tothefor;

/**
* @Author DragonOne
* @Date 2022/1/28 22:05
* @墨水记忆 www.tothefor.com
*/
public class Address {
private String address;
}

Student.java 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.tothefor;

import java.util.*;

/**
* @Author DragonOne
* @Date 2022/1/28 22:05
* @墨水记忆 www.tothefor.com
*/
public class Student {
private String name; //String类型
private Address address; //引用类型
private String[] books; //数组类型
private List<String> hobbys; //list类型,list注入
private Map<String,String> card; //map类型,map注入
private Set<String> games; //set类型,set注入
private String wife; //null注入
private Properties info; //Properties注入
}

📢注意:需自行添加setter和getter、toString方法。

普通注入

1
2
3
4
5
6
7
8
9
10
<bean id="student" class="com.tothefor.Student">
<property name="name" value="loong"/> //也可以设置为空:value=""
</bean>

//或者
<bean id="student" class="com.tothefor.Student">
<property name="name">
<value>loong2</value>
</property>
</bean>

引用注入

1
2
3
4
5
6
7
<bean id="address" class="com.tothefor.Address">
<property name="address" value="四川"/>
</bean>

<bean id="student" class="com.tothefor.Student">
<property name="address" ref="address"/>
</bean>

数组注入

1
2
3
4
5
6
7
8
9
10
<bean id="student" class="com.tothefor.Student">
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
</bean>

List注入

1
2
3
4
5
6
7
8
9
<bean id="student" class="com.tothefor.Student">
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>RAP</value>
</list>
</property>
</bean>

Map注入(特殊)

1
2
3
4
5
6
7
8
<bean id="student" class="com.tothefor.Student">
<property name="card">
<map>
<entry key="身份证" value="1111111111"/>
<entry key="电话号码" value="22222222"/>
</map>
</property>
</bean>

Set注入

1
2
3
4
5
6
7
8
9
<bean id="student" class="com.tothefor.Student">
<property name="games">
<set>
<value>LOL</value>
<value>LCL</value>
<value>LDL</value>
</set>
</property>
</bean>

null注入

1
2
3
4
5
<bean id="student" class="com.tothefor.Student">    
<property name="wife">
<null/>
</property>
</bean>

Properties注入(特殊)

1
2
3
4
5
6
7
8
9
<bean id="student" class="com.tothefor.Student">         
<property name="info">
<props>
<prop key="学号">20220128</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>

所有的都是有一定规律的,除了特殊的两个,而这两个特殊的也是有区别的,需要额外注意。

完整代码

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="address" class="com.tothefor.Address">
<property name="address" value="四川"/>
</bean>

<bean id="student" class="com.tothefor.Student">
<property name="name">
<value>loong2</value>
</property>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>RAP</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="1111111111"/>
<entry key="电话号码" value="22222222"/>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>LCL</value>
<value>LDL</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="学号">20220128</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>

</beans>

测试

1
2
3
4
5
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student stu = (Student) context.getBean("student");
System.out.println(stu.toString());
}

输出:(为方便查看,手动进行了换行)

1
2
3
4
5
6
7
8
9
10
Student{
name='loong2',
address=Address{address='四川'},
books=[西游记, 水浒传, 红楼梦, 三国演义],
hobbys=[唱歌, 跳舞, RAP],
card={身份证=1111111111, 电话号码=22222222},
games=[LOL, LCL, LDL],
wife='null',
info={学号=20220128, password=root, username=root}
}

本文作者: 墨水记忆
本文链接: https://tothefor.com/DragonOne/acc575c8.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!