Spring中Bean的完整生命周期
1) instantiate bean对象实例化
2) populate properties 封装属性
3) 如果Bean实现BeanNameAware 执行 setBeanName
4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6) 如果Bean实现InitializingBean 执行 afterPropertiesSet
7) 调用<bean init-method="XXX"> 指定初始化方法
8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
9) 执行业务处理
10)如果Bean实现 DisposableBean 执行 destroy
11)调用<bean destroy-method="XXX"> 指定销毁方法
接口
public interface ILifecycle {
public void method();
}
实现类
public class LifecycleImpl implements ILifecycle, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
public LifecycleImpl() {
System.out.println("1) instantiate bean对象实例化");
}
public void myInit() {
System.out.println("7) 调用<bean init-method=\"myInit\"> 指定初始化方法 ");
}
public void myDestroy(){
System.out.println("11)调用<bean destroy-method=\"myDestroy\"> 指定销毁方法 ");
}
@Override
public void method() {
System.out.println("9) 执行业务处理");
}
public void setProperty(String info) {
System.out.println("2) populate properties 封装属性");
}
@Override
public void setBeanName(String name) {
System.out.println("3) 如果Bean实现BeanNameAware 执行 setBeanName.--->" + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext.--->" + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("6) 如果Bean实现InitializingBean 执行 afterPropertiesSet ");
}
@Override
public void destroy() throws Exception {
System.out.println("10)如果Bean实现 DisposableBean 执行 destroy");
}
}
后处理bean,对容器内所有bean有效
public class BeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
System.out.println("5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization");
return bean;
}
}
配置
<bean id="lifecycle" class="demo02.LifecycleImpl" init-method="myInit" destroy-method="myDestroy">
<property name="property" value="注入"></property>
</bean>
<bean class="demo02.BeanProcessor"></bean>
测试
@Test
public void demo1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
LifecycleImpl bean = context.getBean("lifecycle",LifecycleImpl.class);
bean.method();
context.close();
}
/**
1) instantiate bean对象实例化
2) populate properties 封装属性
3) 如果Bean实现BeanNameAware 执行 setBeanName.--->lifecycle
4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext.--->org.springframework.context.support.ClassPathXmlApplicationContext@6fc5f743: startup date [Mon Sep 07 22:27:22 CST 2015]; root of context hierarchy
5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6) 如果Bean实现InitializingBean 执行 afterPropertiesSet
7) 调用<bean init-method="myInit"> 指定初始化方法
8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
9) 执行业务处理
22:27:22,535 INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6fc5f743: startup date [Mon Sep 07 22:27:22 CST 2015]; root of context hierarchy
22:27:22,535 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@731d2572: defining beans [lifecycle,demo02.BeanProcessor#0]; root of factory hierarchy
10)如果Bean实现 DisposableBean 执行 destroy
11)调用<bean destroy-method="myDestroy"> 指定销毁方法
*/
可以在实现BeanPostProcessor类里对原有业务方法进行增强处理
public class BeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization");
return bean;
}
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
System.out.println("8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization");
if (beanName.equals("lifecycle")) {
return 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("method")) {
System.out.println("增强method=======================================");
}
return method.invoke(bean, args);
}
});
}
return bean;
}
}
输出:
/**
1) instantiate bean对象实例化
2) populate properties 封装属性
3) 如果Bean实现BeanNameAware 执行 setBeanName.--->lifecycle
4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext.--->org.springframework.context.support.ClassPathXmlApplicationContext@2dec8909: startup date [Tue Sep 08 09:10:45 CST 2015]; root of context hierarchy
5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6) 如果Bean实现InitializingBean 执行 afterPropertiesSet
7) 调用<bean init-method="myInit"> 指定初始化方法
8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
增强method=======================================
9) 执行业务处理
09:10:45,408 INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@2dec8909: startup date [Tue Sep 08 09:10:45 CST 2015]; root of context hierarchy
09:10:45,408 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7e859a68: defining beans [lifecycle,demo02.BeanProcessor#0]; root of factory hierarchy
10)如果Bean实现 DisposableBean 执行 destroy
11)调用<bean destroy-method="myDestroy"> 指定销毁方法
*/