前言:

本文内容:Spring配置说明、DI依赖注入环境、依赖注入的Set注入

推荐免费Spring5基础教程视频:【狂神说Java】Spring5最新完整教程IDEA版通俗易懂_哔哩哔哩_bilibili

Spring配置说明

别名

别名alias

1
2
<!--    别名 alias-->
<alias name="user" alias="users"/>

Bean的配置

1
2
3
4
5
6
7
8
<!--    bean的配置
id: bean 的唯一标识符 也就相当于我们学的对象名
class:bean 对象所对应的类型
name: 也是别名 而且name可以取多个别名
-->
<bean id="userT" class="com.jokerdig.pojo.User" name="userT2,userNew2">
<property name="name" value="Joker大雄"/>
</bean>

Import

import一般用于团队开发,它可以将多个配置文件,导入合并为一个

在resources创建applicationContext.xml

1
2
3
4
5
6
7
8
<?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">

<!--可以通过import引入多个配置文件合并为一个,一般适用于团队开发-->
<import resource="beans.xml"/>
</beans>

DI依赖注入环境

构造器注入

无参构造和有参构造,上一篇已经讲过

1
2
3
4
<!--    这里本质使用实体类的无参构造 默认-->
<bean name="user" class="com.jokerdig.pojo.User">
<property name="name" value="Joker大雄"/>
</bean>

Set方式注入(重点)

  • 依赖注入:Set注入
    • 依赖:bean对象创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入

测试环境搭建

  1. 复杂类型

    1
    Map,Set,List,实体类,Properties.....
  2. 创建测试对象

    新建一个Maven项目模块

    创建StudentAddress实体类

    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
    96
    package 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;
    }

    @Override
    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
    24
    package 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;
    }

    @Override
    public String toString() {
    return "Address{" +
    "address='" + address + '\'' +
    '}';
    }
    }
  3. 创建beans.xml文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?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="student" class="com.jokerdig.pojo.Student">
    <!-- 第一种 普通注入 value-->
    <property name="name" value="张三"/>
    </bean>
    </beans>
  4. 简单测试环境搭建

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package 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 {
    @Test
    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
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
<?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">

<!-- 引入address-->
<bean id="address" class="com.jokerdig.pojo.Address">
<property name="address" value="陕西西安"/>
</bean>

<bean id="student" class="com.jokerdig.pojo.Student">
<!-- 第一种 普通注入 value-->
<property name="name" value="张三"/>
<!-- 第二种 Bean注入 ref -->
<property name="address" ref="address"/>
<!-- 数组注入 property ->array -> value -->
<property name="books">
<array>
<value>三国</value>
<value>红楼梦</value>
<value>福尔摩斯</value>
<value>西游记</value>
<value>狂人日记</value>
</array>
</property>
<!-- List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>打游戏</value>
<value>打篮球</value>
<value>跑步</value>
</list>
</property>
<!-- map-->
<property name="card">
<map>
<entry key="身份证" value="612729199901010001"/>
<entry key="学号" value="20212B3321"/>
<entry key="编号" value="033412"/>
</map>
</property>
<!-- set-->
<property name="games">
<set>
<value>武士:零</value>
<value>Muse Dash</value>
<value>蔚蓝</value>
</set>
</property>
<!-- properties key=value-->
<property name="info">
<props>
<prop key="序号">0001</prop>
<prop key="性别"></prop>
<prop key="username">root</prop>
</props>
</property>
<!-- null-->
<property name="not">
<null/>
</property>
</bean>
</beans>

普通注入

1
2
3
4
5
<bean id="student" class="com.jokerdig.pojo.Student">
<!-- 第一种 普通注入 value-->
<property name="name" value="张三"/>
<!--.......-->
</bean>

Bean注入

1
2
<!--        第二种 Bean注入 ref -->
<property name="address" ref="address"/>

数组(Array)注入

1
2
3
4
5
6
7
8
9
10
<!--        数组注入 property ->array -> value -->
<property name="books">
<array>
<value>三国</value>
<value>红楼梦</value>
<value>福尔摩斯</value>
<value>西游记</value>
<value>狂人日记</value>
</array>
</property>

数组(List)注入

1
2
3
4
5
6
7
8
9
10
<!--        List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>打游戏</value>
<value>打篮球</value>
<value>跑步</value>
</list>
</property>

Map注入

1
2
3
4
5
6
7
8
<!--        map-->
<property name="card">
<map>
<entry key="身份证" value="612729199901010001"/>
<entry key="学号" value="20212B3321"/>
<entry key="编号" value="033412"/>
</map>
</property>

Set注入

1
2
3
4
5
6
7
8
<!--        set-->
<property name="games">
<set>
<value>武士:零</value>
<value>Muse Dash</value>
<value>蔚蓝</value>
</set>
</property>

Properties注入

1
2
3
4
5
6
7
8
<!--        properties key=value-->
<property name="info">
<props>
<prop key="序号">0001</prop>
<prop key="性别"></prop>
<prop key="username">root</prop>
</props>
</property>

NULL注入

1
2
3
4
<!--        null-->
<property name="not">
<null/>
</property>

测试注入结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package 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 {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
// 输入注入结果
System.out.println(student.toString());
}
}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Student{
name='张三',
address=Address{
address='陕西西安'
},
books=[三国, 红楼梦, 福尔摩斯, 西游记, 狂人日记],
hobbys=[听歌, 看电影, 打游戏, 打篮球, 跑步],
card={
身份证=612729199901010001,
学号=20212B3321,
编号=033412
},
games=[武士:零, Muse Dash, 蔚蓝],
info={
序号=0001,
性别=男,
username=root
},
not='null'
}

Process finished with exit code 0