spring容器创建对象的生命周期

本文详细介绍了Spring容器如何管理Bean的生命周期,包括对象的初始化、销毁过程,以及单例和多例对象的不同生命周期。通过配置文件的init-method和destroy-method属性控制初始化和销毁方法的执行,并展示了在关闭容器时不同生命周期的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面我们提到了,

IOC的概念:

把对象的创建、初始化、销毁等工作交给spring容器来完成。

对象的创建我们在前面几个例子已经讨论到了,现在,我们来讨论一下

对象的初始化,销毁

对象的初始化和销毁,也是有spring来完成,可是我们之前在执行的时候,没有看见它们的踪影。
对于了解这个问题,可以设置spring的配置文件的bean中的init-method和destroy-method的取值。
首先,我们要在相应的类中创建好初始化及销毁的方法。名字可以任意取,因为等下要配置到配置文件中。
类中的代码:


public class helloSpring implements Serializable {
    //客户端掉用这个方法
    public void hello(){
        System.out.println("hello spring!");
    }
    //默认构造函数
    public helloSpring() {
        System.out.println("new instance");
    }
     //初始化调用这个方法
    public void init(){
        System.out.println("init+++++++++++++++++");
    }
     //摧毁调用这个方法
    public void destory(){
        System.out.println("destory-----------------");
    }
}

配置文件中的代码:

<!-- 
        init-method:指该bean对应的类中的初始化调用的方法
        destroy-method:指该bean对应的类中的摧毁调用的方法
     -->          
    <bean class="cn.ansel.domain.helloSpring" id="hello"  init-method="init" destroy-method="destory" ></bean>

测试类的代码:

public class testHelloSpring {
    /**
     * 测试对象的创建、初始化、摧毁是否由spring完成
     */
    @Test
    public void test1(){
        //启动spring容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        //得到helloSpring对象
        helloSpring helloSpring=(helloSpring) applicationContext.getBean("hello");

        //调用该对象的方法
        helloSpring.hello();
    }
}

运行测试类的结果
这里写图片描述
从运行结果我们可以看到,spring容器一开启之后,就创建bean的对象,然后再执行初始化方法,然后由客户端调用相应的方法。可是在这里,我们没有看到spring容器调用它的摧毁方法。
我们在调用完hello方法之后,再调用close方法,却找不到。
针对这个问题,我们只需要将applicationContext向下转型之后,就可以调用其摧毁方法了。
修改完代码之后:

public class testHelloSpring {
    /**
     * 测试对象的创建、初始化、摧毁是否由spring完成
     */
    @Test
    public void test1(){
        //启动spring容器
        ApplicationContext Context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //得到helloSpring对象
        helloSpring helloSpring=(helloSpring) Context.getBean("hello");
        //调用该对象的方法
        helloSpring.hello();
        //把上面得到的context向下转型
        ClassPathXmlApplicationContext applicationContext=(ClassPathXmlApplicationContext)Context;
        //关闭spring容器
        applicationContext.close();
    }
}

再次运行,
这里写图片描述

当关闭spring容器的时候,spring容器调用destory方法。

多例对象的生命周期:

当我们把配置文件中的scope取值变成多例的时候,看看程序会怎样运行:
配置文件的代码:

<bean class="cn.ansel.domain.helloSpring" id="hello"  init-method="init" destroy-method="destory" scope="prototype"></bean>

相关类和测试类的代码不变,然后运行测试类:
这里写图片描述
从运行结果我们可以看到,在多例的情况下没有执行destory方法,但是看运行结果图可以知道,spring容器确实是调用了关闭的方法。并且下面那句话是摧毁单例对象,并没有摧毁多例对象。

总结:

spring容器的生命周期:
(a)在单例情况下:
1、由spring容器创建对象
2、spring容器调用初始化方法
3、客户点调用该对象的某些方法
4、关闭spring容器的时候,执行摧毁方法

(b)在多例的情况下:
1、由spring容器创建对象
2、spring容器调用初始化方法
3、客户点调用该对象的某些方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值