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生命周期理解

  • 相关阅读:
    别了,DjVu!
    DjVu转PDF
    我的电子书历程
    连续翻页浏览器面临的共同问题
    对超过2TB的硬盘进行分区需要使用parted
    DB2常用命令
    CentOS增加网卡
    mysql相关参数解读
    max_user_connections参数设置试验
    mysql最大连接数试验
  • 原文地址:https://www.cnblogs.com/xcmelody/p/11080452.html
Copyright © 2011-2022 走看看