【Spring5】Spring5基础教程(3)
前言:
本文内容:Spring配置说明、DI依赖注入环境、依赖注入的Set注入
推荐免费Spring5基础教程视频:【狂神说Java】Spring5最新完整教程IDEA版通俗易懂_哔哩哔哩_bilibili
Spring配置说明
别名
别名alias
1 | <!-- 别名 alias--> |
Bean的配置
1 | <!-- bean的配置 |
Import
import一般用于团队开发,它可以将多个配置文件,导入合并为一个
在resources创建applicationContext.xml
1 |
|
DI依赖注入环境
构造器注入
无参构造和有参构造,上一篇已经讲过
1 | <!-- 这里本质使用实体类的无参构造 默认--> |
Set方式注入(重点)
- 依赖注入:Set注入
- 依赖:bean对象创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
测试环境搭建
-
复杂类型
1
Map,Set,List,实体类,Properties.....
-
创建测试对象
新建一个
Maven
项目模块创建
Student
和Address
实体类Student.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96package com.jokerdig.pojo;
import java.util.*;
/**
* @author Joker大雄
* @data 2022/5/12 - 19:28
**/
public class Student {
private String name;
private Address address;
private String[]books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String not;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getNot() {
return not;
}
public void setNot(String not) {
this.not = not;
}
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", info=" + info +
", not='" + not + '\'' +
'}';
}
}Address.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.jokerdig.pojo;
/**
* @author Joker大雄
* @data 2022/5/12 - 19:28
**/
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
} -
创建
beans.xml
文件1
2
3
4
5
6
7
8
9
10
<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="student" class="com.jokerdig.pojo.Student">
<!-- 第一种 普通注入 value-->
<property name="name" value="张三"/>
</bean>
</beans> -
简单测试环境搭建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package com.jokerdig.test;
import com.jokerdig.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Joker大雄
* @data 2022/5/12 - 19:36
**/
public class SpringTest {
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}运行结果
1
2
3张三
Process finished with exit code 0
依赖注入的Set注入
beans.xml
1 |
|
普通注入
1 | <bean id="student" class="com.jokerdig.pojo.Student"> |
Bean注入
1 | <!-- 第二种 Bean注入 ref --> |
数组(Array)注入
1 | <!-- 数组注入 property ->array -> value --> |
数组(List)注入
1 | <!-- List--> |
Map注入
1 | <!-- map--> |
Set注入
1 | <!-- set--> |
Properties注入
1 | <!-- properties key=value--> |
NULL注入
1 | <!-- null--> |
测试注入结果
1 | package com.jokerdig.test; |
运行结果
1 | Student{ |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hey,Joker!
评论
ValineTwikoo