Bean创建过程
- 实例化bean 设置bean属性
- Aware(注入Bean ID、BeanFactory和AppCtx)
- BeanPostProcessor(s).postProcessBeforeInitialization
- InitializingBean(s).afterPropertiesSet
- 定制的Bean init方法
- BeanPostProcessor(s).postProcessAfterInitialization
- Bean初始化完毕
Bean销毁过程
- 若实现了DisposableBean接口,则会调用destory方法
- 若配置了destory-method属性,则会调用其配置的销毁方法
各种接口方法分类
- Bean自身的方法
- Bean本身调用的方法
- 通过配置文件中的init-method和destroy-method指定的方法
- Bean级生命周期接口方法
- BeanNameAware
- BeanFactoryAware
- InitializingBean
- DiposableBean
- 容器级生命周期接口方法
- BeanPostProcessor
- InstantiationAwareBeanPostProcessor
- 工厂后处理器接口方法(容器级,在应用上下文装配配置文件之后立即调用)
- AspectJWeavingEnabler
- ConfigurationClassPostProcessor
- CustomAutowireConfigure
测试Demo
package com.example.spring.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class LifeCycleBean implements BeanPostProcessor, BeanNameAware,
BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
private String name;
public LifeCycleBean() {
System.out.println("-----------------------1.Bean构造器");
}
public void setName(String name) {
System.out.println("-----------------------2.Bean属性注入");
this.name = name;
}
@Override
public void setBeanName(String name) {
System.out.println("-----------------------3.BeanNameAware的setBeanName()");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("-----------------------4.BeanClassLoaderAware的setBeanClassLoader()");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("-----------------------5.BeanFactoryAware的setBeanFactory()");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("-----------------------6.[初始化之前]BeanPostProcessor的postProcessBeforeInitialization()");
return bean;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("-----------------------7.[初始化中,属性注入后]InitializingBean的afterPropertiesSet()");
}
public void initMethod() {
System.out.println("-----------------------8.调用<bean>的init-method属性自定义的初始化方法");
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("-----------------------9.[初始化之后]BeanPostProcessor的postProcessAfterInitialization()");
return bean;
}
public void normalMethod() {
System.out.println("-----------------------10.[容器初始化完成]程序运行,当JVM关闭时,销毁容器");
}
@Override
public void destroy() throws Exception {
System.out.println("-----------------------11.[容器销毁]DisposableBean的destroy()");
}
public void destroyMethod() {
System.out.println("-----------------------12.[容器销毁]调用<bean>的destory-method属性自定义的销毁方法");
}
public String getName() {
return name;
}
}
- spring-bean-life-cycle.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">
<bean id="lifeCycleBean" class="com.example.spring.bean.LifeCycleBean" destroy-method="destroyMethod" init-method="initMethod">
<property name="name" value="name"/>
</bean>
</beans>
public class SpringTest {
/**
* 测试Bean生命周期
* */
@Test
public void testBeanLifeCycle() {
ClassPathResource resource = new ClassPathResource("spring-bean-life-cycle.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.addBeanPostProcessor(new LifeCycleBean());
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
LifeCycleBean lifeCycleBean = (LifeCycleBean) factory.getBean("lifeCycleBean");
lifeCycleBean.normalMethod();
factory.destroySingleton("lifeCycleBean");
}
}