zoukankan      html  css  js  c++  java
  • Bean生命周期

    package xyz.pascall.spring.learn.component;
    
    import lombok.ToString;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.core.Ordered;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    /**
     * SpringBeanPostProcessorLive
     * <p>
     * 一:实例化
     *      BeanFactory: 第一次请求时,初始化bean通过调用createBean实例化
     *      ApplicationContext: 容器启动结束后,通过BeanDefinition实例化所用bean
     * 二:设置对象属性(依赖注入)
     *      (bean被封装到BeanWrapper中)Spring根据BeanDefinition和BeanWrapper提供的属性设置接口完成依赖注入
     * 三:处理Aware接口
     *      1.ApplicationContextAware(*)
     *      2.ApplicationEventPublisherAware
     *      3.BeanClassLoaderAware(*)
     *      4.BeanFactoryAware
     *      5.BeanNameAware
     *      6.EmbeddedValueResolverAware
     *      7.EnvironmentAware(*)
     *      8.ImportAware
     *      9.LoadTimeWeaverAware
     *      10.MessageSourceAware
     *      11.NotificationPublisherAware
     *      12.ResourceLoaderAware
     *      13.ServletConfigAware
     *      14.ServletContextAware
     * 四:init-method(@init-method)
     * 五:InitializingBean(bean属性设置之后接口)
     * 六:BeanPostProcessor.postProcessBeforeInitialization(Object, String)
     * 七:BeanPostProcessor.postProcessAfterInitialization(Object, String)
     * =====================================================================
     * 八:DisposableBean
     * 九:destroy-method
     *
     *
     * =====================================================================
     * =====================================================================
     * 1.工厂后置处理器(容器级)
     *      BeanFactoryPostProcessor
     *          AspectJWeavingEnabler
     *          ConfigurationClassPostProcessor
     *          CustomAutowireConfigurer
     * 2.容器级别生命周期处理器(实现类“后处理器”)
     *      InstantiationAwareBeanPostProcessor
     *      BeanPostProcessor
     * 3.Bean级生命周期接口方法
     *      BeanNameAware
     *      BeanFactoryAware
     *      InitializingBean
     *      DiposableBean
     * 4.Bean自身的方法
     *      init-method
     *      destroy-method
     *
     * *********************************************************************
     * *********************************************************************
     * BeanFactoryPostProcessor加载顺序:
     *      1.PriorityOrdered
     *      2.Ordered
     *      3.无排序
     *
     *    Spring解析处理所有@Configuration标签类,并将Bean定义注册到BeanFactory中
     *      1.internalConfigurationAnnotationProcessor  -> ConfigurationClassPostProcessor      -> PriorityOrdered
     *
     *    Spring通过EventListenerMethodProcessor来处理@EventListener注解
     *    ** 自定义listener使用@EventListener注解在监听的方法上
     *    ** Spring通过EventListenerMethodProcessor解析配置类包含@EventListener的方法
     *    ** 通过DefaultEventListenerFactory将其转化为ApplicationListenerMethodAdapter#ApplicationListenerMethodAdapter对象
     *      1.internalEventListenerProcessor            -> EventListenerMethodProcessor
     * *********************************************************************
     * *********************************************************************
     * BeanPostProcessor加载顺序:
     *      1、找到所有实现PriorityOrdered的BeanPostProcessor,然后getBean,然后统一排序,然后beanFactory.addBeanPostProcessor(..)
     *        2、找到所有实现Ordered的BeanPostProcessor,然后getBean,然后统一排序,然后beanFactory.addBeanPostProcessor(..)
     *        3、找到所有没实现排序接口的BeanPostProcessor,不需要sort,直接beanFactory.addBeanPostProcessor(..)
     *      4、最后注册一个特殊的处理器,beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
     *
     *    Spring定义的没有排序的BeanPostProcessor
     *      1.ApplicationContextAwareProcessor(*)   -> 比较特殊,处理感知接口Aware的BeanPostProcessor,被提前实例化
     *      2.ApplicationListenerDetector           -> 检查作用在Bean上的BeanPostProcessor的数量是否足够,否则PostProcessorRegistrationDelegate$BeanPostProcessorChecker
     *      3.ServletContextAwareProcessor
     *      4.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
     *
     *    Spring中与注解@Autowired/@Resource有关的BeanPostProcessor最先被加载
     *      1.internalAutowiredAnnotationProcessor  -> AutowiredAnnotationBeanPostProcessor     -> PriorityOrdered
     *      2.internalCommonAnnotationProcessor     -> CommonAnnotationBeanPostProcessor        -> PriorityOrdered
     * *********************************************************************
     * *********************************************************************
     * InstantiationAwareBeanPostProcessor
     * *********************************************************************
     * *********************************************************************
     *
     * @author Jie Zhang, 2019/7/30
     * @version DEMO v1.0
     */
    @Component
    @Slf4j
    @ToString
    public class SpringBeanPostProcessorLive implements BeanNameAware, BeanPostProcessor, InitializingBean, DisposableBean, Ordered {
    
        private ComponentDemo componentDemo;
    
        /**
         * 1.初始化
         */public SpringBeanPostProcessorLive() {
            log.info("1.SpringBeanPostProcessorLive.SpringBeanPostProcessorLive()");
        }
    
        /**
         * 3.处理Aware接口(BeanNameAware)
         *
         * @param name
         */
        @Override
        public void setBeanName(String name) {
            log.info("3.BeanNameAware.setBeanName(String name)");
        }
    
        /**
         * 4.init-method
         */
        @PostConstruct
        public void init() {
            log.info("4.init-method");
        }
    
        /**
         * 5.InitializingBean
         *
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            log.info("5.InitializingBean.afterPropertiesSet()");
        }
    
        /**
         * 6.处理bean后置处理器前置方法
         *
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            log.info("6.BeanPostProcessor.postProcessBeforeInitialization(Object bean, String beanName)");
            log.info(beanName);
            if (bean instanceof ComponentDemo) {
                this.componentDemo = (ComponentDemo)bean;
            }
            return bean;
        }
    
        /**
         * 7.处理bean后置处理器后置方法
         *
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            log.info("7.BeanPostProcessor.postProcessAfterInitialization(Object bean, String beanName)");
            log.info(beanName);
            return bean;
        }
    
    
        /////////////////////////////////////////////////////////////////////////////////////////////////
    
        /**
         * 8.DisposableBean
         * @throws Exception
         */
        @Override
        public void destroy() throws Exception {
            log.info("8.DisposableBean.destroy()");
        }
    
        /**
         * 9.destroy-method
         */
        @PreDestroy
        public void destroyMethod() {
            log.info("9.destroy-method");
        }
    
        @Override
        public int getOrder() {
            return Integer.MAX_VALUE;
        }
    }

     参考文章:

    【小家Spring】Spring IOC容器启动流程 AbstractApplicationContext#refresh()方法源码分析(一)

  • 相关阅读:
    < java.util >-- Set接口
    Codeforces 627 A. XOR Equation (数学)
    Codeforces 161 B. Discounts (贪心)
    Codeforces 161 D. Distance in Tree (树dp)
    HDU 5534 Partial Tree (完全背包变形)
    HDU 5927 Auxiliary Set (dfs)
    Codeforces 27E. Number With The Given Amount Of Divisors (暴力)
    lght oj 1257
    Codeforces 219D. Choosing Capital for Treeland (树dp)
    Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
  • 原文地址:https://www.cnblogs.com/pascall/p/11269609.html
Copyright © 2011-2022 走看看