hooyantsing's Blog

第108次课程_Spring_IOC原理

字数统计: 2.2k阅读时长: 12 min
2020/02/18

源辰76班

第108次课程

2020.02.18

内容

Spring_IOC原理

项目路径:

1.创建工程

1.常规创建Maven工程

2.pom.xml导入Spring-Contents包

maven仓库网址: https://mvnrepository.com/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<properties>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
       <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
  
  <dependencies>
    <!--  https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
        <version>5.1.3.RELEASE</version>
    </dependency>
    
  </dependencies>

3.工程内创建Beans.xml文件

参考网址: https://docs.spring.io/spring/docs/5.1.13.RELEASE/spring-framework-reference/core.html#spring-core

Beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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
         https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    <bean id="..." class="...">   
    </bean>
    <bean id="..." class="...">
    </bean>
    -->
</beans>

2.Spring基础

set方法对bean对象设值

构造方法对bean对象设置

基础bean类

User.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.yc.mybatis.bean;
import java.util.Date;
public class User {
    private Integer id;
    private String username;
    private String password;
    private String email;
    private String headImg;
    private String phone;
    private Integer status;
    private Date regtime;
    
    private Hall hall;
    /*
     * 构造方法(构造器)
     */
    public User() {}
    
    public User(Integer id,String  username,String password) {
        super();
        System.out.println("===== Integer  id,String username,String password =====");
        this.id = id;
        this.username = username;
        this.password = password;
    }
    //重载
    public User(String username,String  password,Integer id) {
        super();
        System.out.println("===== String  username,String password,Integer id =====");
        this.id = id;
        this.username = username;
        this.password = password;
    }
    
    /*
     * 初始化方法
     */
    public void initA() {
        System.out.println("=====initA=====");
    }
    
    /*
     * 销毁方法
     */
    public void close() {
        System.out.println("=====close=====");
    }
    
    /*
     * 静态工厂方法
     */
    public static User create() {
        System.out.println("=====create=====");
        return new User();
    }
    
    /*
     * 动态工厂方法
     */
    public User create1() {
        System.out.println("=====create1=====");
        return new User();
    }
    
    
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username)  {
        this.username = username == null ?  null : username.trim();
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password)  {
        this.password = password == null ?  null : password.trim();
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email == null ? null :  email.trim();
    }
    public String getHeadImg() {
        return headImg;
    }
    public void setHeadImg(String headImg) {
        this.headImg = headImg == null ?  null : headImg.trim();
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone == null ? null :  phone.trim();
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public Date getRegtime() {
        return regtime;
    }
    public void setRegtime(Date regtime) {
        this.regtime = regtime;
    }
    public Hall getHall() {
       return hall;
    }
    public void setHall(Hall hall) {
       this.hall = hall;
    }
}

Hall.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
package com.yc.mybatis.bean;
public class Hall {
    private Integer id;
    private String name;
    private String hallType;
    private Integer capacity;
    private Integer cinemaId;
    
    private Cinema cinema;
    public Cinema getCinema() {
       return cinema;
    }
    public void setCinema(Cinema cinema) {
       this.cinema = cinema;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name == null ? null :  name.trim();
    }
    public String getHallType() {
        return hallType;
    }
    public void setHallType(String hallType)  {
        this.hallType = hallType == null ?  null : hallType.trim();
    }
    public Integer getCapacity() {
        return capacity;
    }
    public void setCapacity(Integer  capacity) {
        this.capacity = capacity;
    }
    public Integer getCinemaId() {
        return cinemaId;
    }
    public void setCinemaId(Integer  cinemaId) {
        this.cinemaId = cinemaId;
    }
}

Cinema.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
package com.yc.mybatis.bean;
import java.util.List;
public class Cinema {
    private Integer id;
    private String name;
    private String address;
    private String gps;
    private String img;
    
    private List<Hall> halls;
    public List<Hall> getHalls() {
       return halls;
    }
    public void setHalls(List<Hall> halls) {
       this.halls = halls;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name == null ? null :  name.trim();
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address == null ?  null : address.trim();
    }
    public String getGps() {
        return gps;
    }
    public void setGps(String gps) {
        this.gps = gps == null ? null :  gps.trim();
    }
    public String getImg() {
        return img;
    }
    public void setImg(String img) {
        this.img = img == null ? null :  img.trim();
    }
}

Spring设置文件

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
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
<?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
         https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- bean详解 -->
    <!--
       id         bean对象标识
       class      
       scope      作用范围    {   singleton(默认,单例公用唯一一个)
                              prototype(原型,每次获取的bean都是一个新创建的bean)
                              request(请求期间获取的bean 唯一的)
                              session(会话期间获取的bean 唯一的)
                              global(全局与一种特殊的应用有关 )
                          }
       depends-on 值:另一个bean的id。依赖属性,表示当前的bean必须依赖其他的bean
       destroy-method 用于指定销毁方法,会在对象被销毁时执行,也就是容器关闭时
       init-method    用于指定初始化方法,会在对象创建的时被执行,也就是容器启动时
       lazy-init  默认请求下所有的对象都在容器启动时创建(所以容器启动很慢),可通过该属性设置延迟加载,延迟到getBean时加载
       primary    主要bean配置,同一类型的bean出现多次,必须设置该属性为为true
       factory-method 设置工厂方法(动态和静态之分),如果单独使用factory-method,那么就是静态工厂
       factory-bean 值:另一个bean的id   factory-method + factory-bean = 动态工厂
    
     -->
    <bean id="user"                          
         class="com.yc.mybatis.bean.User"
         scope="prototype"
         init-method="initA"
         destroy-method="close"   
         lazy-init="true"
         primary="true"
         factory-method="create"
    ></bean>  
    
    <bean id="user1"
         class="com.yc.mybatis.bean.User"
         factory-method="create1"
         factory-bean="user"
    ></bean>
    
    <!--  ================================================================== -->
    
    <!-- 向bean内set数据 -->
    <!--
       1.set方法注入
       2.构造方法(构造器)注入
       3.接口注入
     -->
    <bean id="user2"  class="com.yc.mybatis.bean.User">
       <!-- 1.set方法注入 -->
       <property name="id"  value="1"></property>
       <property name="username" value="武松"></property>
    </bean>
    
    <bean id="user3"  class="com.yc.mybatis.bean.User">
       <!-- 2.构造方法(构造器)注入 -->
       <constructor-arg name="username"  value="李逵" index="1"></constructor-arg>
       <constructor-arg name="id" value="1"  index="0"></constructor-arg>
       <constructor-arg name="password"  value="123" index="2"></constructor-arg>
       
       <property name="hall"  ref="hall"></property>
    </bean>
    
    <!-- ref 表示引用其他bean,写bean的id -->
    <bean id="user4"  class="com.yc.mybatis.bean.User">
       <property name="hall"  ref="hall"></property>
    </bean>
    <bean id="hall"  class="com.yc.mybatis.bean.Hall">
       <property name="name" value="小象"></property>
    </bean>
    
    <!-- bean对象中放入list对象 -->
    <bean id="cinema"  class="com.yc.mybatis.bean.Cinema">
       <property name="halls">
           <list>
               <bean  class="com.yc.mybatis.bean.Hall">
                  <property name="name"  value="红旗"></property>
               </bean>
               <bean  class="com.yc.mybatis.bean.Hall">
                  <property name="name"  value="万达"></property>
               </bean>
               <bean  class="com.yc.mybatis.bean.Hall">
                  <property name="name"  value="红色"></property>
               </bean>
               <bean  class="com.yc.mybatis.bean.Hall">
                  <property name="name"  value="进步"></property>
               </bean>
           </list>
       </property>
    </bean>
    
</beans>

Junit测试类

Test1.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
package com.yc.spring.bean;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yc.mybatis.bean.Cinema;
import com.yc.mybatis.bean.User;

public class Test1 {

    //导入Spring配置文件
    private ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    
    @Test
    public void test() {
        System.out.println("-------------测试开始------------");
        //使用id获取bean需要做强制类型转换
        User user1 = (User) context.getBean("user");
        Assert.assertNotNull(user1);
        User user2 = context.getBean(User.class);
        User user3 = context.getBean(User.class);
        Assert.assertNotEquals(user1, user2);
        Assert.assertNotEquals(user1, user3);
        //默认情况下,getBean同一个bean,返回同一个对象 user1 == user2
        
        System.out.println("------------------------------");
        //测试动态的工厂方法
        User user4 = (User) context.getBean("user1");
    }
    
    @Test
    public void test1() {
        User user = (User) context.getBean("user2");
        Assert.assertEquals("武松",user.getUsername());
        Assert.assertEquals((Integer)1,user.getId());
        
        User user1 = (User) context.getBean("user3");
        Assert.assertEquals("李逵",user1.getUsername());
        Assert.assertEquals((Integer)1,user.getId());
        
        Assert.assertEquals("小象",user1.getHall().getName());
        
        User user2 = (User) context.getBean("user4");
        Assert.assertEquals(user1.getHall(),user2.getHall());
    }
    
    @Test
    public void test2() {
        Cinema c = context.getBean(Cinema.class);
        Assert.assertEquals(4, c.getHalls().size());
        Assert.assertEquals("进步",c.getHalls().get(3).getName());
    }
}

附加:Maven导入包

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependencies>
    <!--  https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
        <version>4.3.17.RELEASE</version>
    </dependency>
    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.13</version>
    </dependency>
</dependencies>
CATALOG