# 11-bean生命周期

回到spring导航页

IoC的初始化第一步为解析ApplicationContext.xml文件,看一下ApplicationContext.xml文件中需要创建哪些对象,而且为哪些对象注入哪些属性,紧接着就是对象实例化,执行构造函数,IoC会根据xml文件通过反射实例化对应的bean,同时基于java的规则,对应的构造方法也会执行,接下来就是为对象注入属性,也就是为当前新建的对象注入哪些属性,当对象注入以后,由IoC容器会自动的调用对象的init-method初始化方法来完成这个对象的初始化工作,(这里的疑问解释:对象初始化不是应该在对象的构造方法中来完成吗?那为什么这里又出现了一个init-method的配置呢?原因是:在构造方法创建的时候,作为这个对象它还没有任何属性值,只有当这个对象创建好之后,IOC容器才为其注入对象数据,那init-method的作用非常明显:他就是在为对象注入属性值以后,基于这个属性值来完成这个对象的初始化工作,这也是init-method方法用意所在),这时IoC的初始化工作就完成了,之后就可以写业务代码了,当IoC结束的时候,IoC容器准备销毁,同时调用在配置文件中声明的destroy-method方法来释放对应的资源,当所有的IoC容器中的bean的destroy-method都执行完后,IoC容器销毁完毕 bean的生命周期

# 演示bean的生命周期

package com.torey.spring.ioc.entity;

import lombok.Data;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/4/30 15:12
 * @描述:
 */
@Data
public class Order {
    //单价
    private float price;
    //数量
    private Integer quantity;
    //总价=单价*数量
    private float total;

    public Order() {
        System.out.println("无参构造函数-创建order对象");
    }

    public void init(){
        System.out.println("======执行init方法=====");
        this.total=price*quantity;
    }
    public void destroy(){
        System.out.println("========容器销毁是执行-释放与订单对象相关的资源(资源:可以是文件||网络的连接)========");
    }
    public void pay(){
        System.out.println("订单金额为:" + total);
    }

    public void setPrice(float price) {
        System.out.println("setPrice=" + price);
        this.price = price;
    }

    public void setQuantity(Integer quantity) {
        System.out.println("setQuantity=" + quantity);
        this.quantity = quantity;
    }

    public void setTotal(float total) {
        this.total = total;
    }
}

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

applicationContext.xml文件内容如下:

<?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">
    <!--init-method="init"是在设置完属性之后,执行的方法-->
    <!--//registerShutdownHook是销毁IoC容器的方法,这个registerShutdownHook()执行的过程中,会自动的调用destroy-method="destroy"中的方法-->
    <bean id="order1" class="com.torey.spring.ioc.entity.Order" init-method="init" destroy-method="destroy">
        <property name="price" value="5.6"/>
        <property name="quantity" value="10"/>
    </bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
package com.torey.spring.ioc;

import com.torey.spring.ioc.dao.UserDao;
import com.torey.spring.ioc.entity.Order;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/4/30 7:23
 * @描述:
 */
public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context=
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Order order = context.getBean("order1", Order.class);
        order.pay();
        //registerShutdownHook是销毁IoC容器的方法,这个registerShutdownHook()执行的过程中,会自动的调用destroy-method="destroy"中的方法
        ((ClassPathXmlApplicationContext) context).registerShutdownHook();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

结果输出为:

无参构造函数-创建order对象
setPrice=5.6
setQuantity=10
======执行init方法=====
订单金额为:56.0
========容器销毁是执行-释放与订单对象相关的资源(资源:可以是文件||网络的连接)========
1
2
3
4
5
6

# 导航,上一页,下一页

10beanscope属性讲解
12自己实现一个极简的IoC容器

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者