摘要
SpringBean的生命期包括创建、复位和消毁,其中BeanPostProcessor回调函数和Aware方式的回调函数机会都很关键。复位和消毁的回调函数次序也很重要。详细了解BeanPostProcessor可以更好地掌握SpringBean的生命周期。
正文
深层次源代码了解SpringBean生命期
简述
文中叙述下Spring的创建对象、复位、消毁,全部SpringBean生命期,聊一聊BeanPostProcessor的回调函数机会、Aware方式的回调函数机会、复位方式的回调函数以及次序、消毁方式的回调函数以及次序、关键的BeanPostProcessor的详细介绍。
开头是一张我画的启用运转图,随后是我写的一个Demo根据日志打印出了SpringBean的生命期,最终根据源代码渐渐地跟踪其生命期。
生命期运转图
生命期Demo
以下对某一个Bean开展getBean实际操作,最终消毁前后文,根据日志来查询SpringBean的生命期
编码
package com.deepz.spring;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Slf4j
@Configuration
public class BeanLifeCycleManager {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanLifeCycleManager.class);
context.getBean("beanLifeCycle");
context.close();
}
@Bean(initMethod = "init", destroyMethod = "destroyMethod")
public BeanLifeCycle beanLifeCycle() {
return new BeanLifeCycle();
}
interface MyAware extends ApplicationContextAware, EnvironmentAware, BeanFactoryAware {
}
@Component
static class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
if ("beanLifeCycle".equals(beanName)) {
log.info(">>>>>>>>>>元信息收集 ,MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) \nbeanDefinition = [{}]\n,beanType = [{}],beanName = [{}]\n", beanDefinition, beanType, beanName);
}
}
}
@Component
static class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if ("beanLifeCycle".equals(beanName)) {
log.info(">>>>>>>>>>创建对象前,InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation(Class<?> beanClass,String beanName) \nbeanClass = [{}],beanName = [{}]\n", beanClass, beanName);
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if ("beanLifeCycle".equals(beanName)) {
log.info(">>>>>>>>>>创建对象后,InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation(Object bean, String beanName)\nbean = [{}],beanName = [{}]\n", bean, beanName);
}
return false;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("beanLifeCycle".equals(beanName)) {
log.info(">>>>>>>>>>复位前,InstantiationAwareBeanPostProcessor#postProcessBeforeInitialization(Object bean, String beanName)\nbean= [{}],beanName = [{}]\n", bean, beanName);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("beanLifeCycle".equals(beanName)) {
log.info(">>>>>>>>>>复位后,InstantiationAwareBeanPostProcessor#postProcessAfterInitialization(Object bean, String beanName)\nbean= [{}],beanName = [{}]\n", bean, beanName);
}
return bean;
}
}
public static class BeanLifeCycle implements InitializingBean, MyAware, DisposableBean {
public void init() {
log.info(">>>>>>>>>>init-method\n");
}
@PostConstruct
public void postConstruct() {
log.info(">>>>>>>>>>postConstruct\n");
}
@Override
public void afterPropertiesSet() throws Exception {
log.info(">>>>>>>>>>afterPropertiesSet\n");
}
public void destroyMethod() {
log.info(">>>>>>>>>>destroy-method\n");
}
@Override
public void destroy() {
log.info(">>>>>>>>>>DisposableBean-destroy\n");
}
@PreDestroy
public void preDestroy(){
log.info(">>>>>>>>>>preDestroy\n");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
log.info(">>>>>>>>>>BeanFactoryAware#setBeanFactory\n");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.info(">>>>>>>>>>ApplicationContextAware#setApplicationContext\n");
}
@Override
public void setEnvironment(Environment environment) {
log.info(">>>>>>>>>>EnvironmentAware#setEnvironment\n");
}
}
}
实行結果
创建对象前
从createBean逐渐,印证Bean的创建对象全过程,最先是Bean创建对象前的一个拓展点,它容许你自定回到Bean案例。(AOP也是在这儿转化成代理商目标的)
回调函数Bean创建对象前的方式
AbstractAutowireCapableBeanFactory#createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
主要是为了更好地回调函数全部InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation(Class<?> beanClass, String beanName)方式,该方式会回到Object目标,假如回到的Object不以空,则会回调函数全部BeanPostProcessor的postProcessAfterInitialization(Object bean, String beanName)方式,那麼回到的Object则会做为Bean去解决,假如回到Null,那麼事后便会交给Spring来创建对象、复位(doCreateBean)。
自定阻拦创建对象Bean后回调函数Bean后置摄像头方式
创建对象
AbstractAutowireCapableBeanFactory#doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
如源代码所显示,假如以上拓展点沒有return,那麼便会进到到doCreateBean方式
最先是对Bean开展创建对象,在其中包含了构造器推论等,文中但是多聊这方面內容,最终会回到BeanWrapper包囊的Bean案例。
元信息收集
创建对象以后Spring根据回调函数MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)对一些元信息内容干了搜集维护保养解决,如@Autowire、@Resource、@PostConstruct 和 @PreDestroy等,为事后特性引入做准备。
MergedBeanDefinitionPostProcessor的完成类
MergedBeanDefinitionPostProcessor回调函数
复位
创建对象完后,对一些必须搜集的信息内容也做好准备,事后便是开展特性引入和回调函数复位方式了,在其中populateBean方式是特性添充,initializeBean是回调函数复位方式。
InstatiationAwareBeanPostProcessor回调函数postProcessAfterInstantiation方式
AbstractAutowireCapableBeanFactory#populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw)
Aware插口回调函数
AbstractAutowireCapableBeanFactory#initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
一部分Aware插口回调函数、BeanPostProcessor的复位外置回调函数(包含PostConstruct的启用、其他Aware的回调函数)、afterPropertiesSet回调函数、自定init方式回调函数、BeanPostProcessor的复位后置摄像头回调函数
一部分Aware回调函数
AbstractAutowireCapableBeanFactory#invokeAwareMethods(final String beanName, final Object bean
BeanPostProcessor的复位外置回调函数
关键BeanPostProcessor以下:
ApplicationContextAwareProcessor#postProcessBeforeInitialization(final Object bean, String beanName)回调函数剩下Aware方式
InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization(final Object bean, String beanName)回调函数PostConstruct方式
回调函数复位方式
AbstractAutowireCapableBeanFactory#invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
先回调函数InitializingBean的afterPropertiesSet方式,接着回调函数自定的init-method
BeanPostProcessor的复位后置摄像头回调函数
消毁
消毁方式最后会来到DisposableBeanAdapter的destroy方式去做解决,与复位方式相近,这儿简易详细介绍把。
看图片就能发觉,次序实行的,起先注释方式,随后是DisposableBean的回调函数,最终是自定的消毁方式,便是这般简易。
(低声)弄了挺久的,假如对您有协助,或是使你追忆推进有关知识要点了,帮我点个”适用“激励下..(搞笑),有哪些难题热烈欢迎评价探讨。。。
关注不迷路
扫码下方二维码,关注宇凡盒子公众号,免费获取最新技术内幕!
评论0