zoukankan      html  css  js  c++  java
  • Spring Bean的一生

    Spring Bean的一生

    When you work directly in Java, you can do anything you like with your objects and do not always need to rely on the container lifecycle.

    前言:

    在Ioc容器启动后相应的Bean并没有立即实例化,此时Ioc容器仅仅拥有所有对象的BeanDefinition(Bean对象在Spring中的描述,包含该Bean在容器中实例化所需的信息,通过解析xml/javaconfig后所得)

    Spring Bean生命周期的管理

    Spring bean factory能管理Beans的生命周期,通过实现InitializingBeanDisposableBean

    这两个接口都只声明一个方法让我们可以初始化/关闭bean中的资源

    当然在整个生命周期中也提供了一些Call Back Methods,这些方法类似于Servlet中的监听器的实现

    • Bean处于post-initialization阶段时,我们可以通过实现InitializingBean接口来实现afterPropertiesSet()
    • Bean处于pre-destruction 阶段时,我们可以实现DisposableBean 接口来实现destroy()方法

    Spring Bean的生命周期图

    Code Time

    Rick类交给Spring容器管理,方法顺序为其在Spring容器中的执行顺序

    Rick.java

    public class Rick implements InitializingBean, DisposableBean {
        public Rick(){
            System.out.println("Rick is in [Rick] - Rick()");
        }
    
        public void customInit(){
            System.out.println("Rick is in [Rick] - customInit()");
        }
    
        @PostConstruct
        public void postConstruct(){
            System.out.println("Rick is in [@PostConstruct] - postConstruct()");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("Rick is in [InitializingBean] - afterPropertiesSet()");
        }
    
        @PreDestroy
        public void preDestroy(){
            System.out.println("Rick is in [@PreDestroy] - preDestroy()");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("Rick is in [DisposableBean] - destroy()");
        }
    
        public void customDestroy(){
            System.out.println("Rick is in [Rick] - customDestroy()");
        }
    }
    

    加入BeanPostProcess

    RickBeanPostProcess.java

    public class RickBeanPostProcess implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof Rick) {
                System.out.println("Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()");
            }
    
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof Rick) {
                System.out.println("Rick is in [BeanPostProcessor] - postProcessAfterInitialization()");
            }
            return bean;
        }
    }
    

    配置类

    RickConfig.java

    @Configuration
    public class RickConfig {
    
        /**
         * 指定自定义初始化方法和自定义销毁方法
         * @return Rick
         */
        @Bean(initMethod = "customInit",destroyMethod = "customDestroy")
        public Rick rick(){
            return new Rick();
        }
    
        @Bean
        public RickBeanPostProcess rickBeanPostProcess(){
            return new RickBeanPostProcess();
        }
    }
    

    测试类

    Client.java

    public class Client {
        public static void main(String[] args) {
            ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RickConfig.class);
            ((AnnotationConfigApplicationContext) applicationContext).close();
        }
    }
    

    最终结果如下

    Rick is in [Rick] - Rick()
    Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()
    Rick is in [@PostConstruct] - postConstruct()
    Rick is in [InitializingBean] - afterPropertiesSet()
    Rick is in [Rick] - customInit()
    Rick is in [BeanPostProcessor] - postProcessAfterInitializa
    Rick is in [@PreDestroy] - preDestroy()
    Rick is in [DisposableBean] - destroy()
    Rick is in [Rick] - customDestroy()
    

    参考

    Spring Framework Documentation

    Spring Bean Life Cycle Explained

    Spring Bean Life Cycle

    Spring Boot Bean生命周期

    Spring的Bean生命周期理解

  • 相关阅读:
    你读了该博客中哪些超链接?有何感想
    最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助?
    1500802028 王莉娟
    解码方法
    N皇后问题
    两个链表的交叉
    全排列
    交叉字符串
    翻转链表
    爬楼梯
  • 原文地址:https://www.cnblogs.com/xcmelody/p/11080452.html
Copyright © 2011-2022 走看看