本文描述下Spring的實例化、初始化、銷燬,整個SpringBean生命週期,聊一聊BeanPostProcessor的回調時機、Aware方法的回調時機、初始化方法的回調及其順序、銷燬方法的回調及其順序、重要的BeanPostProcessor的介紹。
開頭是一張我畫的調用流轉圖,而後就是我寫的一個Demo經過日誌打印了SpringBean的生命週期,最後經過源碼慢慢跟進其生命週期。java
以下對某一個Bean進行getBean操做,最後銷燬上下文,經過日誌來查看SpringBean的生命週期spring
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生成代理對象的入口之一,但基本不會走這裏)app
AbstractAutowireCapableBeanFactory#createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
ide
主要是爲了回調全部InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation(Class<?> beanClass, String beanName)方法,該方法會返回Object對象,若是返回的Object不爲空,則會回調全部BeanPostProcessor的postProcessAfterInitialization(Object bean, String beanName)方法,那麼返回的Object則會做爲Bean去處理,若是返回Null,那麼後續就會交由Spring來實例化、初始化(doCreateBean)。
post
AbstractAutowireCapableBeanFactory#doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
如源碼所示,若是上述擴展點沒有return,那麼就會進入到doCreateBean方法
3d
首先是對Bean進行實例化,其中包括了構造器推斷等,本文不過多聊這塊內容,最後會返回BeanWrapper包裹的Bean實例。
代理
實例化以後Spring經過回調MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)對一些元信息作了收集維護處理,如@Autowire、@Resource、@PostConstruct 和 @PreDestroy等,爲後續屬性注入作準備。
日誌
實例化完了,對一些須要收集的信息也準備好了,後續就是進行屬性注入和回調初始化方法了,其中populateBean方法是屬性填充,initializeBean是回調初始化方法。
code
AbstractAutowireCapableBeanFactory#populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw)
對象
AbstractAutowireCapableBeanFactory#initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
AbstractAutowireCapableBeanFactory#invokeAwareMethods(final String beanName, final Object bean
AbstractAutowireCapableBeanFactory#invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
先回調InitializingBean的afterPropertiesSet方法,隨後回調自定義的init-method
注意:AOP生成代理的主要入口即是這裏的了,對應的BeanPostProcessor->AbstractAutoProxyCreator。(後續單聊AOP相關源碼,FLAG)
銷燬方法最終會走到DisposableBeanAdapter的destroy方法去作處理,與初始化方法相似,這裏簡單介紹把。
看圖就能發現,順序執行的,先是註解方法,而後是DisposableBean的回調,最後是自定義的銷燬方法,就是如此簡單。
(小聲)弄了挺久的,若是對你有幫助,或者讓你回憶鞏固相關知識點了,給我點個"支持「鼓勵下..(滑稽),有什麼問題歡迎評論討論。。。