Spring:Bean配置的生命周期之“初始化init”“销毁destroy”

本文详细介绍了Spring中Bean的生命周期,特别是初始化和销毁的方法。包括在XML配置中使用`init-method`和`destroy-method`属性,以及实现InitializingBean和DisposableBean接口调用`afterPropertiesSet()`和`destroy()`方法。通过三个示例展示了不同配置下初始化和销毁的执行顺序,强调了可选择性方法的特点及执行优先级。

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

初始化两种常见方法:

1.在xml配置bean中写入:init-method="start",start为类中的初始化方法。

2.在类中实例化InitializingBean接口,重写接口的 afterPropertiesSet()方法。

销毁的两种常见方法:

1.在xml配置bean中写入:destroy-method="stop"。stop为类中的销毁方法。

2.在类中实例化DisposableBean接口,重新destroy()方法。

第三种方法:可选择性方法,分为两步

第一步,在xml文件的配置中,配置两个默认方法

default-init-method="defautInit" default-destroy-method="defaultDestroy"

第二步,在类中写相应的方法。

下面分别就三种方法举例:

第一种方法,xml配置。

//1.首先配置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.xsd">
        <!-- init-method="start" destroy-method="stop" -->
        <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop" ></bean>
	
 </beans>
//2.在类中写相应的方法
package com.imooc.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BeanLifeCycle implements InitializingBean, DisposableBean {

	public void start() {
		System.out.println("Bean start .");
	}
	
	public void stop() {
		System.out.println("Bean stop.");
	}
	
}
//3.测试
package com.imooc.test.lifecycle;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifecycle extends UnitTestBase {
	
	public TestBeanLifecycle() {
		super("classpath:spring-lifecycle.xml");
	}
	
	@Test
	public void test1() {
		super.getBean("beanLifeCycle");
	}
	
}

测试结果为:

8月 01, 2018 2:53:04 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:53:04 CST 2018]; root of context hierarchy
8月 01, 2018 2:53:04 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean start .
8月 01, 2018 2:53:05 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:53:04 CST 2018]; root of context hierarchy
Bean stop.

可以看到在loading和closing后分别执行了我们写在类中的初始化和destroy方法。

 

第二种方法:

//1.xml文件不需要写入配置
//2.类中需要实例化两个接口,并且重写方法
package com.imooc.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle implements InitializingBean, DisposableBean {
	
	@Override
	public void destroy() throws Exception {
		System.out.println("Bean destroy.");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Bean afterPropertiesSet.");
	}
	
}

运行测试后的结果为:

8月 01, 2018 2:56:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:56:46 CST 2018]; root of context hierarchy
8月 01, 2018 2:56:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean afterPropertiesSet.
8月 01, 2018 2:56:46 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:56:46 CST 2018]; root of context hierarchy
Bean destroy.

同样是在loading和closing后分别执行了初始化和destroy方法。

 

第三种方法:

//1.在xml文件的配置中设置默认的初始化和destroy配置,如倒数第三行所示
<?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" 
        default-init-method="defautInit" default-destroy-method="defaultDestroy">

        <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" ></bean>
	
 </beans>
package com.imooc.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle{
	
	public void defautInit() {
		System.out.println("Bean defautInit.");
	}
	
	public void defaultDestroy() {
		System.out.println("Bean defaultDestroy.");
	}

}

测试结果:

8月 01, 2018 2:59:25 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:59:25 CST 2018]; root of context hierarchy
8月 01, 2018 2:59:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean defautInit.
8月 01, 2018 2:59:25 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:59:25 CST 2018]; root of context hierarchy
Bean defaultDestroy.

注意:三个方法各有不同,与方法一和方法二比,方法三是可选择性方法,也就是即使xml文件中写了相应的配置,仍然可以不写方法,不去执行。但是如果按方法一去写bean的配置的话,就必须在类中写相应的方法。

另外如果三个方法同时有的话,默认方法(方法三)不会执行,方法2在方法1之前执行。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值