Spring 一文带你速懂Bean生命周期(singleton 和 prototype)

Bean生命周期(singleton 和 prototype)

关于Bean生命周期:
Spring 根据 Bean 的作用域来选择管理方式。对于 singleton 作用域的 Bean,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁;而对于 prototype 作用域的 Bean,Spring 只负责创建,当容器创建了 Bean 的实例后,Bean 的实例就交给客户端代码管理,Spring 容器将不再跟踪其生命周期。

那如何验证呢?

首先,你需要看一篇基础知识,大约需要5分钟,然后我们做个测试,答案大家自然就能明白Bean的生命周期。

链接: Spring Bean生命周期.

首先我们先测试单例模式singleton

代码如下

<?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-3.0.xsd">

    <bean id="helloWorld" class="net.biancheng.HelloWorld" scope="singleton"
    	init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World!" />
    </bean>

</beans>
package net.biancheng;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("message : " + message);
    }
    
    private void init() {
		System.out.println("测试xml配置的初始化方法");
	}
    
    private void destroy() {
    	System.out.println("测试xml配置的销毁方法");
	}
}
package net.biancheng;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
    	//Spring实例
//        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    	AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.setMessage("对象A");//注意1-getBean两次对象
        obj.getMessage();
        context.registerShutdownHook();//注意2-只调用一次销毁
        
        //测试单例模式和原型模式
        HelloWorld obj2 = (HelloWorld) context.getBean("helloWorld");
        obj2.getMessage();
    }
}
测试xml配置的初始化方法
message : 对象A
message : 对象A
测试xml配置的销毁方法

所以,验证了:对于 singleton 作用域的 Bean,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁;

Spring 知道Bean 所有生命周期,所以它调用了初始和销毁方法,又因为验证了单例是容器内只存在一个对象,所以只存在调用一次初始化方法,并且销毁方法最后才调用。

最后我们测试原型模式prototype

我们只改动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
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="helloWorld" class="net.biancheng.HelloWorld" scope="prototype"
    	init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World!" />
    </bean>

</beans>

var foo = 'bar';
测试xml配置的初始化方法
message : 对象A
测试xml配置的初始化方法
message : Hello World!

所以,验证了:对于 prototype 作用域的 Bean,Spring 只负责创建,当容器创建了 Bean 的实例后,Bean 的实例就交给客户端代码管理,Spring 容器将不再跟踪其生命周期。

Spring 只负责创建,所以它调用了初始而不会调用销毁方法,又同时验证了原型模式每次通过 Spring 容器获取 Bean 时,容器都会创建一个 Bean 实例,所以会调用两次初始化方法。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值