• Spring底层原理学习笔记--第三讲--(bean生命周期与模板方法)


    bean生命周期

    A03Application.java

    package com.lucifer.itheima.a03;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    
    @SpringBootApplication
    public class A03Application {
        public static void main(String[] args) {
            //执行结果为
            //  2023-11-06 10:41:10.761  INFO 54360 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 构造
            //  2023-11-06 10:41:10.764  INFO 54360 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 依赖注入
            //  2023-11-06 10:41:10.765  INFO 54360 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 初始化
            //  2023-11-06 10:41:11.060  INFO 54360 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 销毁
            ConfigurableApplicationContext context = SpringApplication.run(A03Application.class);
            context.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    LifeCycleBean.java

    package com.lucifer.itheima.a03;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    
    @Component
    @Slf4j
    public class LifeCycleBean {
    
        public LifeCycleBean(){
            log.info("构造");
        }
    
        @Autowired
        public void autowire(@Value("${JAVA_HOME}") String home) {
            log.info("依赖注入:{}",home);
        }
    
        @PostConstruct
        public void init() {
            log.info("初始化");
        }
    
        @PreDestroy
        public void destroy(){
            log.info("销毁");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    MyBeanPostProcessor.java

    package com.lucifer.itheima.a03;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.PropertyValues;
    import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
    import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @Slf4j
    //执行结果为
    //2023-11-06 11:50:39.728  INFO 47300 --- [           main] c.l.itheima.a03.MyBeanPostProcessor      : <<<<<<实例化之前执行,这里返回的对象会替换掉原来的bean
    //    2023-11-06 11:50:39.730  INFO 47300 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 构造
    //    2023-11-06 11:50:39.732  INFO 47300 --- [           main] c.l.itheima.a03.MyBeanPostProcessor      : <<<<<<实例化之后,这里如果返回false会跳过依赖注入阶段
    //    2023-11-06 11:50:39.732  INFO 47300 --- [           main] c.l.itheima.a03.MyBeanPostProcessor      : <<<<<<初始化之前执行,这里返回的对象会替换掉原来的bean,如PostConstruct,@ConfigurationProperties
    //2023-11-06 11:50:39.732  INFO 47300 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 初始化
    //    2023-11-06 11:50:39.732  INFO 47300 --- [           main] c.l.itheima.a03.MyBeanPostProcessor      : <<<<<<初始化之后执行,这里返回的对象会替换掉原来的bean,如代理增强
    //    2023-11-06 11:50:40.034  INFO 47300 --- [           main] c.l.itheima.a03.MyBeanPostProcessor      : <<<<<<销毁之前执行,如@PreDestroy
    //2023-11-06 11:50:40.035  INFO 47300 --- [           main] com.lucifer.itheima.a03.LifeCycleBean    : 销毁
    public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
    
        @Override
        public void postProcessBeforeDestruction(Object bean,String beanName) throws BeansException{
            if (beanName.equals("lifeCycleBean"))
                log.info("<<<<<<销毁之前执行,如@PreDestroy");
        }
    
        @Override
        public Object postProcessBeforeInstantiation(Class<?> beanClass,String beanName) throws BeansException{
            if (beanName.equals("lifeCycleBean")){
                log.info("<<<<<<实例化之前执行,这里返回的对象会替换掉原来的bean");
            }
            // 返回null的话就不会替换掉原来的对象
            return null;
        }
    
        @Override
        public boolean postProcessAfterInstantiation(Object bean,String beanName) throws BeansException{
            if (beanName.equals("lifeCycleBean")) {
                log.info("<<<<<<实例化之后,这里如果返回false会跳过依赖注入阶段");
                return false;
            }
            return true;
        }
    
        @Override
        public PropertyValues postProcessProperties(PropertyValues pvs,Object bean,String beanName) throws BeansException{
            if (beanName.equals("lifeCycleBean")) {
                log.info("<<<<<<依赖注入阶段执行,如@Autowired,@Value,@Resource");
            }
            return pvs;
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException{
            if (beanName.equals("lifeCycleBean")) {
                log.info("<<<<<<初始化之前执行,这里返回的对象会替换掉原来的bean,如PostConstruct,@ConfigurationProperties");
            }
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException{
            if (beanName.equals("lifeCycleBean")){
                log.info("<<<<<<初始化之后执行,这里返回的对象会替换掉原来的bean,如代理增强");
            }
            return bean;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    模板方法

    package com.lucifer.itheima.a03;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestMethodTemplate {
    
        public static void main(String[] args) {
            MyBeanFactory beanFactory = new MyBeanFactory();
            beanFactory.addBeanPostProcessor(bean-> System.out.println("解析 @Autowired"));
            beanFactory.addBeanPostProcessor(bean-> System.out.println("解析 @Resource"));
            beanFactory.getBean();
    
        }
    
        // 模板方法 Template Method Pattern
        static class MyBeanFactory {
            public Object getBean() {
                Object bean = new Object();
                System.out.println("构造 "+bean);
                System.out.println("依赖注入 "+ bean); // @Autowired,@Resource
                for (BeanPostProcessor processor : processors) {
                    processor.inject(bean);
                }
                System.out.println("初始化 "+bean);
                return bean;
            }
    
            private List<BeanPostProcessor> processors = new ArrayList<>();
    
            public void addBeanPostProcessor(BeanPostProcessor processor) {
                processors.add(processor);
    
            }
        }
        static interface BeanPostProcessor{
            public void inject(Object bean); //对依赖注入阶段的扩展
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    计算属性computed、过滤器定义Vue.filter()
    定时备份mysql数据库
    【请求报错:javax.net.ssl.SSLHandshakeException: No appropriate protocol】
    TEE侧信道综述(源于论文)
    升级pip 升级pip3的快速方法
    LeetCode-232. 用栈实现队列(C++)
    图扑软件数字孪生民航飞联网,构建智慧民航新业态
    面试系列多线程:Tomcat线程池的原理
    多分类交叉熵理解
    VS编译.cu文件源文件无法打开matrix.h和mex.h问题
  • 原文地址:https://blog.csdn.net/weixin_42594143/article/details/134246421