一、Spring生命周期概述
我们知道一个对象的生命周期:创建(实例化-初始化)-使用-销毁,而在Spring中,Bean对象周期当然遵从这一过程,但是Spring提供了许多对外接口,允许开发者对三个过程(实例化、初始化、销毁)的前后做一些操作。
在Spring Bean中,实例化是为bean对象开辟空间(具体可以理解为构造函数的调用),初始化则是对属性的初始化,说的具体点,这里的属性初始化应该是属性的注入(构造函数也可以有属性的初始化语句,但不属于这一部分),属性注入是通过setter方法注入属性(不管是注解方式还是bean配置property属性方式,其实质都是通过属性的setter方法实现的)。
二、Spring生命周期流程图
1、instantiate bean 对象实例化
2、populate properties 封装属性
3、如果Bean实现BeanNameAware,执行setBeanName
4、如果Bean实现BeanFactoryAware或ApplicationContextAware,设置工厂setBeanFactory或上下文对象setApplicationContext
5、如果存在类实现BeanPostPrcess(处理Bean),执行postProcessBeforeInitialization
6、如果Bean实现InitializingBean,执行afterPropertiesSet
7、调用自定义的init-method方法
8、如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization
9、执行业务处理
10、如果Bean实现DisposableBean,执行destroy方法
11、调用自定义的destroy-method方法
三、Spring生命周期演示
3.1 applicationContext配置文件
<!--
init-method : 初始化方法
destroy-method : 销毁方法
-->
<bean id="beanLifeCycle" class="com.spring.bean.BeanLifeCycle" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="test"></property>
</bean>
<!--
指定BeanPostProcessor的实现类
-->
<bean class="com.spring.bean.MyBeanPostProcess"></bean>
3.2 BeanLifeCycle类 --- 生命周期演示类
package com.spring.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class BeanLifeCycle implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String name;
public BeanLifeCycle() {
System.out.println("第一步:实例化BeanLifeCycle对象");
}
// 自定义功能
public void add() {
System.out.println("第九步:自定义功能add.....");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("第二步:属性name的注入" + name);
this.name = name;
}
@Override
public void setBeanName(String name) {
System.out.println("第三步:得到bean的id或name值:" + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("第四步:得到ApplicationContext对象:" + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("第六步:属性注入完成后");
}
public void myInit() {
System.out.println("第七步:自定义的init方法");
}
@Override
public void destroy() throws Exception {
System.out.println("第十步:执行destroy方法");
}
public void myDestroy() {
System.out.println("第十一步:执行自定义的销毁方法");
}
}
3.3 MyBeanPostProcess类 --- BeanPostProcessor的实现类
package com.spring.bean;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcess implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("第五步:beanPostProcessor的before方法");
System.out.println("处理的bean是" + bean + " 它的名称是" + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
System.out.println("第八步:beanPostProcessor的after方法");
if (beanName.equals("beanLifeCycle")) {
// 使用动态代理可以处理bean的功能增强
Object proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("add")) {
long currentTimeMillis = System.currentTimeMillis();
Object invoke = method.invoke(bean, args);
long currentTimeMillis2 = System.currentTimeMillis();
System.out.println(currentTimeMillis2 - currentTimeMillis);
return invoke;
} else {
return method.invoke(bean, args);
}
}
});
}
return bean;
}
}
3.4 测试类
// 测试bean的生命周期
@Test
public void test7() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanLifeCycle beanLife = (BeanLifeCycle) applicationContext.getBean("beanLifeCycle");
beanLife.add();
applicationContext.close();
}
3.5 输出结果
第一步:实例化BeanLifeCycle对象
第二步:属性name的注入test
第三步:得到bean的id或name值:beanLifeCycle
第四步:得到ApplicationContext对象:org.springframework.context.support.ClassPathXmlApplicationContext@625b99ed: startup date [Sun Jan 14 19:31:57 CST 2018]; root of context hierarchy
第五步:beanPostProcessor的before方法
处理的bean是com.spring.bean.BeanLifeCycle@b926d29 它的名称是beanLifeCycle
第六步:属性注入完成后
第七步:自定义的init方法
第八步:beanPostProcessor的after方法
第九步:自定义功能add.....
第十步:执行destroy方法
第十一步:执行自定义的销毁方法