本文描述下Spring的实例化、初始化、销毁,整个SpringBean生命周期,聊一聊BeanPostProcessor的回调时机、Aware方法的回调时机、初始化方法的回调及其顺序、销毁方法的回调及其顺序、重要的BeanPostProcessor的介绍。
开头是一张我画的调用流转图,然后就是我写的一个Demo通过日志打印了SpringBean的生命周期,最后通过源码慢慢跟进其生命周期。
如下对某一个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"); } } } 执行结果