参考:https://www.cnblogs.com/zrtqsk/p/3735273.html
这里的流程图可以和最后的输出结果对比,逐一对比来了解Spring Bean的生命周期。输出结果已经经过很认真的整理,以及去除不必要的日志 ,并且对输出结果做了优化,容易理解。
Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:
Bean自身的方法: 这个包括了Bean本身调用的方法和通过配置文件中的init-method和destroy-method指定的方法
Bean级生命周期接口方法:这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法
容器级生命周期接口方法:这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
工厂后处理器接口方法:这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
public class Demo implements BeanFactoryAware, ApplicationContextAware,
BeanNameAware, InitializingBean, DisposableBean {
private BeanFactory beanFactory;
private String beanName;
private ApplicationContext ioc;
private int name;
public Demo() {
System.out.println("调用Demo构造方法...");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("[BeanFactoryAware]... setBeanFactory : " + beanName);
this.beanFactory = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("[ApplicationContextAware]... setIOC : " + applicationContext);
this.ioc = applicationContext;
}
@Override
public void setBeanName(String s) {
System.out.println("[BeanNameAware]... setBeanName : " + s);
this.beanName = s;
}
@Override
public void destroy() throws Exception {
System.out.println("[DisposableBean]... ");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("[InitializingBean]... afterPropertiesSet()");
}
public void myInit() {
System.out.println("[init-method]...");
}
public void myDestroy() {
System.out.println("[destroy-method]...");
}
@Override
public String toString() {
return "Demo{" +
"beanFactory=" + beanFactory +
", beanName='" + beanName + '\'' +
", ioc=" + ioc +
", name=" + name +
'}';
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public String getBeanName() {
return beanName;
}
public ApplicationContext getIoc() {
return ioc;
}
public void setIoc(ApplicationContext ioc) {
this.ioc = ioc;
}
public int getName() {
return name;
}
public void setName(int name) {
System.out.println("注入name属性...");
this.name = name;
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor() {
super();
System.out.println("BeanFactoryPostProcessor的实现类构造器");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("BeanFactoryPostProcessor调用postProcessorBeanFactory方法");
BeanDefinition bd = configurableListableBeanFactory.getBeanDefinition("demo");
bd.getPropertyValues().addPropertyValue("name", "100");
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
System.out.println("MyBeanPostProcessor的构造函数");
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("[postProcessor]... postProcessAfterInitialization");
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("[postProcessor]... postProcessBeforeInitialization");
return bean;
}
}
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import java.beans.PropertyDescriptor;
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
public MyInstantiationAwareBeanPostProcessor() {
super();
System.out.println("MyInstantiationAwareBeanPostProcessor... 的构造函数");
}
/**
* 实例化bean前调用
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
return super.postProcessBeforeInitialization(bean, beanName);
}
/**
* 实例化bean之后调用
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInstantiation方法");
return super.postProcessAfterInitialization(bean, beanName);
}
/**
* 设置某个属性时调用
* @param pvs
* @param pds
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
}
}
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xxx.springboot.springBeanTest"/>
<bean id="beanPostProcessor" class="com.xxx.springboot.springBeanTest.MyBeanPostProcessor"/>
<bean id="instantiationAwareBeanPostProcessor" class="com.xxx.springboot.springBeanTest.MyInstantiationAwareBeanPostProcessor"/>
<bean id="beanFactoryPostProcessor" class="com.xxx.springboot.springBeanTest.MyBeanFactoryPostProcessor"/>
<bean id="demo" class="com.xxx.springboot.springBeanTest.Demo"
scope="singleton" p:name="200"
init-method="myInit" destroy-method="myDestroy" />
beans>
public static void main(String[] args) {
System.out.println("IOC starting...");
ClassPathXmlApplicationContext ioc =
new ClassPathXmlApplicationContext("app.xml");
System.out.println("IOC started...");
Demo demo = ioc.getBean("demo", Demo.class);
System.out.println(demo);
System.out.println("IOC closing...");
ioc.close();
System.out.println("IOC closed...");
}
******************** IOC starting... ********************
18:51:50.401 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2ac1fdc4
18:51:50.649 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 9 bean definitions from class path resource [app.xml]
18:51:50.676 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
18:51:50.699 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
18:51:50.701 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beanFactoryPostProcessor'
BeanFactoryPostProcessor的实现类构造器
18:51:50.702 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
BeanFactoryPostProcessor调用postProcessorBeanFactory方法
18:51:50.704 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
18:51:50.706 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
18:51:50.712 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beanPostProcessor'
MyBeanPostProcessor的构造函数
18:51:50.729 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'instantiationAwareBeanPostProcessor'
MyInstantiationAwareBeanPostProcessor... 的构造函数
18:51:50.738 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'demo'
调用Demo构造方法...
InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法
注入name属性...
[BeanNameAware]... setBeanName : demo
[BeanFactoryAware]... setBeanFactory : demo
[ApplicationContextAware]... setIOC : org.springframework.context.support.ClassPathXmlApplicationContext@2ac1fdc4, started on Wed Dec 30 18:51:50 CST 2020
[postProcessor]... postProcessBeforeInitialization
InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法
[InitializingBean]... afterPropertiesSet()
[init-method]...
[postProcessor]... postProcessAfterInitialization
InstantiationAwareBeanPostProcessor调用postProcessAfterInstantiation方法
******************** IOC started... ********************
Demo{
beanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@704d6e83: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,beanPostProcessor,instantiationAwareBeanPostProcessor,beanFactoryPostProcessor,demo]; root of factory hierarchy, beanName='demo', ioc=org.springframework.context.support.ClassPathXmlApplicationContext@2ac1fdc4, started on Wed Dec 30 18:51:50 CST 2020, name=100}
******************** IOC closing... ********************
18:51:50.815 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@2ac1fdc4, started on Wed Dec 30 18:51:50 CST 2020
[DisposableBean]...
[destroy-method]...
******************** IOC closed... ********************