Bean在Spring容器中的生命周期

晨曦之光 发布于 2012/04/25 16:16
阅读 584
收藏 1

配置在Spring中的Bean在Spring容器中从加载到销毁会经历那些过程呢?如果实现一些特定的Spring接口,这些特定接口的方法会在什么时候被调用呢?本文简单介绍一下这些过程.

Bean在Spring容器中的生命周期如下图所示:


1,调用Bean的构造函数(或者工厂方法)实例化Bean.
2,对Bean的成员变量赋值.
3,如果Bean实现了BeanNameAware,调用Bean的setBeanName方法.
4,如果Bean实现了BeanFactoryAware,调用Bean的setBeanFactory方法.
5,如果Bean实现了ApplicationContextAware,调用Bean的setApplicationContext方法.
6,如果容器中配置了BeanPostProcessor,调用BeanPostProcessor的postProcessBeforeInitialization方法(如果有多个BeanPostProcessor,调用每一个BeanPostProcessor的postProcessBeforeInitialization方法).
6,如果Bean实现了InitializingBean,调用Bean的afterPropertiesSet方法.
7,如果Bean配置了init-method方法,调用init-method配置的Bean方法.
8,如果容器中配置了BeanPostProcessor,调用BeanPostProcessor的postProcessAfterInitialization方法.(如果有多个BeanPostProcessor,调用每一个BeanPostProcessor的postProcessAfterInitialization方法).
9,Bean处于可以使用的状态.
10,Spring容器关闭.
11,4,如果Bean实现了BeanFactoryAware,调用Bean的destroy方法.
12,如果Bean配置了destroy-method方法,调用destroy-method配置的Bean的方法.

实例:LifeCycleBean implements InitializingBean, DisposableBean,BeanNameAware,BeanFactoryAware, ApplicationContextAware,配置了init-method="init" destroy-method="cleanup",容器中有两个BeanPostProcessor

public class LifeCycleBean implements InitializingBean, DisposableBean,
		BeanNameAware,BeanFactoryAware, ApplicationContextAware {
	private String str = "default";
	public LifeCycleBean() {
		System.out.println("construct LifecycleBean ***************");
	}
	public LifeCycleBean(String str) {
		this.str = str;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		System.out.println("setStr ***************");
		this.str = str;
	}
	public void init() {
		System.out.println("init mtd ***************");
	}
	public void cleanup() {
		System.out.println("cleanup mtd ***************");
	}
	
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet ***************");
	}
	public void destroy() throws Exception {
		System.out.println("destroy ***************");
	}	
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		System.out.println("setApplicationContext***************");
	}
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("setBeanFactory***************");
	}
	public void setBeanName(String arg0) {
		System.out.println("setBeanName***************" + arg0);			
	}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("************** MyBeanPostProcessor postProcessBeforeInitialization Bean '" + beanName);
        return bean;
    }
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("************** MyBeanPostProcessor postProcessAfterInitialization Bean '" + beanName);
        return bean;
    }
}
Spring配置
<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
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean class="com.test.spring.beanfactoryCtx.MyBeanPostProcessor" />
	<bean class="com.test.spring.beanfactoryCtx.MyBeanPostProcessor1" />
	<bean name="lifeCycleBean" class="com.test.spring.beanfactoryCtx.LifeCycleBean" init-method="init" destroy-method="cleanup">
		<property name="str" value="test str"/>
	</bean>
</beans>
调用代码:注意在从main方法启动Spring的话,需要 registerShutdownHook才能看到destroy方法和由destroy-method配置的方法的调用.
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:conf/bfactoryCtx1.xml");
		((ClassPathXmlApplicationContext)ctx).registerShutdownHook();
		LifeCycleBean bean1 = ctx.getBean("lifeCycleBean",LifeCycleBean.class);
		System.out.println("***********" + bean1 +  "   " + bean1.getStr());
输出结果如下:
construct LifecycleBean ***************
setStr ***************       #1
setBeanName***************lifeCycleBean
setBeanFactory***************
setApplicationContext***************
************** MyBeanPostProcessor postProcessBeforeInitialization Bean 'lifeCycleBean  #2
************** MyBeanPostProcessor1 postProcessBeforeInitialization Bean 'lifeCycleBean #2
afterPropertiesSet ***************
init mtd ***************
************** MyBeanPostProcessor postProcessAfterInitialization Bean 'lifeCycleBean   #2
************** MyBeanPostProcessor1 postProcessAfterInitialization Bean 'lifeCycleBean  #2
***********com.test.spring.beanfactoryCtx.LifeCycleBean@157fb52   test4                 #3
destroy ***************
cleanup mtd ***************
#1为成员变量赋值的步骤
#2如果有多个BeanPostProcessor的话,每一个BeanPostProcessor都会被调用一次.
#3Bean 被客户端代码使用


原文链接:http://blog.csdn.net/kkdelta/article/details/7282782
加载中
返回顶部
顶部