Spring容器中Bean的生命周期
- 容器中Bean的生命周期
- 容器中Bean的创建过程
- MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition
- InstantiationAwareBeanPostProcessor#postProcessProperties
- BeanPostProcessor#postProcessBeforeInitialization
- AbstractAutowireCapableBeanFactory#invokeAwareMethods
- AbstractAutowireCapableBeanFactory#invokeInitMethods
- BeanPostProcessor#postProcessAfterInitialization
- AbstractBeanFactory#registerDisposableBeanIfNecessary
- 容器中Bean的销毁过程
- 事例
- 参考
圣诞节在家整理了一下Spring容器中的Bean到底是怎么诞生的,又是如何毁灭的
容器中Bean的生命周期
下图为Spring容器中Bean的生命周期
容器中Bean的创建过程
下图是Spring容器创建一个Bean大致的过程
MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition
下图是 CommonAnnotationBeanPostProcessor / AutowiredAnnotationBeanPostProcessor 在此刻做的事情
CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition
- 将@PostConstruct方法缓存至lifecycleMetadataCache(LifecycleMetadata的initMethods)
- 将@PreDestroy方法缓存至lifecycleMetadataCache (LifecycleMetadata的destroyMethods)
- 将@Resouorce、@WebServiceRef、@EJB 修饰的字段或方法缓存至injectionMetadataCache(ResourceElement/WebServiceRefElement/EjbRefElement)
AutowiredAnnotationBeanPostProcessor#postProcessMergedBeanDefinition
将@Autowired、@Value、@javax.inject.Inject 修饰的字段或方法缓存至 injectionMetadataCache(AutowiredFieldElement/AutowiredMethodElement)
InstantiationAwareBeanPostProcessor#postProcessProperties
下图是 CommonAnnotationBeanPostProcessor / AutowiredAnnotationBeanPostProcessor 在此刻做的事情
CommonAnnotationBeanPostProcessor#postProcessProperties
获取injectionMetadataCache中的InjectedElement(ResourceElement/WebServiceRefElement/EjbRefElement )
并进行解析注入
AutowiredAnnotationBeanPostProcessor#postProcessProperties
获取injectionMetadataCache中的InjectedElement(AutowiredFieldElement/AutowiredMethodElement)
并进行解析注入
BeanPostProcessor#postProcessBeforeInitialization
下图是 CommonAnnotationBeanPostProcessor 在此刻做的事情
@PostConstruct方法此刻被调用
AbstractAutowireCapableBeanFactory#invokeAwareMethods
做的事情如图
- BeanNameAware.setBeanName
- BeanClassLoaderAware.setBeanClassLoader
- BeanFactoryAware.setBeanFactory
AbstractAutowireCapableBeanFactory#invokeInitMethods
做的事情如图
- 执行InitializingBean.afterPropertiesSet方法
- 执行invokeCustomInitMethod,即执行init-method
BeanPostProcessor#postProcessAfterInitialization
AbstractBeanFactory#registerDisposableBeanIfNecessary
做的事情如图
- DisposableBean类型的Bean(DisposableBeanAdapter)被放至 DefaultSingletonBeanRegistry的disposableBeans中
- 过滤 DestructionAwareBeanPostProcessor 至 DisposableBeanAdapter的beanPostProcessors
容器中Bean的销毁过程
下图是Spring容器中的Bean销毁过程
AbstractApplicationContext#doClose
程序正常退出后,系统会调用此方法
DestructionAwareBeanPostProcessor#postProcessBeforeDestruction
下图是CommonAnnotationBeanPostProcessor在此刻做的事情
执行invokeDestroyMethods,即执行@PreDestroy方法
DisposableBeanAdapter#invokeCustomDestroyMethod
执行invokeCustomDestroyMethod,即执行destroy-method方法
事例
@Service
public class BeanPropertyResource {
@Override
public String toString() {
return "BeanPropertyResource Instantiation";
}
}
@Service
public class BeanPropertyAutowired {
@Override
public String toString() {
return "BeanPropertyAutowired Instantiation";
}
}
public class BeanLifeCycle implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {
@Resource
private BeanPropertyResource beanPropertyResource;
@Autowired
private BeanPropertyAutowired beanPropertyAutowired;
public BeanLifeCycle() {
System.out.println("BeanLifeCycle");
System.out.println("without beanPropertyResource: " + beanPropertyResource);
System.out.println("without beanPropertyAutowired: " + beanPropertyAutowired);
}
@PostConstruct
public void postConstructMethod() {
System.out.println("with beanPropertyResource: " + beanPropertyResource);
System.out.println("with beanPropertyAutowired: " + beanPropertyAutowired);
System.out.println("postConstructMethod");
}
public void initMethod() {
System.out.println("init-method initMethod");
}
@Override
public void afterPropertiesSet() {
System.out.println("InitializingBean afterPropertiesSet");
}
@PreDestroy
public void preDestroyMethod() {
System.out.println("preDestroyMethod");
}
@Override
public void destroy() {
System.out.println("DisposableBean destroy");
}
public void destroyMethod() {
System.out.println("destroy-method destroyMethod");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("BeanClassLoaderAware");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware");
}
@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware");
}
}
XML中配置
<bean class="cn.oyo.trade.starter.BeanLifeCycle" init-method="initMethod" destroy-method="destroyMethod" />