spring bean的生命周期

1、如果一个bean实现了 BeanNameAware接口  执行setBeanName方法

2、如果一个bean实现了 BeanFactoryAware接口  执行setBeanFactory方法

3、如果一个bean实现了 ApplicationContextAware接口  执行setApplicationContext方法

4、如果一个bean实现了 BeanPostProcessor接口  执行postProcessBeforeInitialization方法

5、如果一个bean实现了 InitializingBean接口  执行afterPropertiesSet方法

6、如果一个bean在xml或@bean注解中配置了 method-init  执行自己的methodInit方法

7、如果一个bean实现了 BeanPostProcessor接口  执行postProcessAfterInitialization方法

8、如果一个bean实现了 DisposableBean接口  执行destroy方法

9、如果一个bean在xml或@bean注解中配置了 mehod-destory 执行自己的mehodDestory方法

需要注意的是BeanPostProcessor  需要单独的一个类去实现BeanPostProcessor接口   如果一个类实现BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean  又实现了BeanPostProcessor接口   bean初始化时的方法会有冲突

在xml中配置的bean  在初始化的时候都会调BeanPostProcessor 的postProcessBeforeInitialization和postProcessAfterInitialization方法

 

代码

public class LifeCycleBeanTest implements
BeanNameAware,
BeanFactoryAware,
ApplicationContextAware,
InitializingBean,
DisposableBean{
	private String name;

	@Override
	public void destroy() throws Exception {
		System.out.println("08 DisposableBean.... ");
		
	}

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

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("03 ApplicationContextAware.... ");
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("02 BeanFactoryAware.... ");
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("01 BeanNameAware.... ");
	}
	
	
	public void methodInit(){
		System.out.println("06 xml method-init...");
	}
	public void mehodDestory(){
		System.out.println("09 xml mehod-destory...");
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) throws Exception{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/beans/factory/xml/lifeCycleBeanTest.xml");
		LifeCycleBeanTest test=context.getBean("LifeCycleBeanTest", LifeCycleBeanTest.class);
		System.out.println("LifeCycleBeanTest 初始化完成");
		context.destroy();
	}
}

public class BeanPostProcessorTest implements BeanPostProcessor{
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("04 BeanPostProcessorBefore...");
		return bean;
	}
	
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("07 BeanPostProcessorAfter...");
		return bean;
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="LifeCycleBeanTest" class="org.springframework.beans.factory.xml.test.LifeCycleBeanTest" init-method="methodInit" destroy-method="mehodDestory">
		<property name="name" value="LifeCycleBean"/>
	</bean>
	<bean id="BeanPostProcessorTest" class="org.springframework.beans.factory.xml.test.BeanPostProcessorTest"></bean>
</beans>

运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值