SpringBoot 测试Bean生命周期

首先上一张自制的自认为比较全的Spring Bean生命周期流程图:SpringBoot 测试Bean生命周期_第1张图片

测试代码:

  1. 测试bean

    @Data
    public class BeanLifeCycle implements InitializingBean, DisposableBean, BeanFactoryAware, BeanNameAware {
    
        private String name;
    
        public BeanLifeCycle() {
            System.out.println("BeanTestLifeCycle no parameters constructor");
        }
    
        public BeanLifeCycle(String name) {
            this.name = name;
            System.out.println("BeanTestLifeCycle parameters constructor");
        }
    
        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            System.out.println("execution BeanFactoryAware.setBeanFactory()");
        }
    
        @Override
        public void setBeanName(String name) {
            System.out.println("execution BeanNameAware.setBeanName()");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("execution InitializingBean.afterPropertiesSet()");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("execution DisposableBean.destroy()");
        }
    
        @PostConstruct
        public void postConstruct() {
            System.out.println("execution @PostConstruct()");
        }
    
        @PreDestroy
        public void preDestroy() {
            System.out.println("execution @PreDestroy()");
        }
    
        public void initMethod() {
            System.out.println("execution @Bean.initMethod()");
        }
    
        public void destroyMethod() {
            System.out.println("execution @Bean.destroyMethod()");
        }
    
    }
    
    
  2. 测试BeanPostProcessor

    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof BeanLifeCycle) {
                System.out.println("execute BeanLifeCycle BeanPostProcessor.postProcessBeforeInitialization()");
            }
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof BeanLifeCycle) {
                System.out.println("execute BeanLifeCycle BeanPostProcessor.postProcessAfterInitialization()");
            }
            return bean;
        }
    }
    
  3. 配置Bean

    @Configuration
    public class BeanConfig {
    
        @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
        public BeanLifeCycle getBeanTestLifeCycle() {
            return new BeanLifeCycle();
        }
    }
    
  4. 运行程序,查看打印结果

    BeanTestLifeCycle no parameters constructor
    execution BeanNameAware.setBeanName()
    execution BeanFactoryAware.setBeanFactory()
    execution @PostConstruct()
    execution InitializingBean.afterPropertiesSet()
    execution @Bean.initMethod()
    2020-05-12 14:37:12.815  INFO 15640 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 5.2.1 by Boxfuse
    2020-05-12 14:37:12.842  INFO 15640 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
    2020-05-12 14:37:13.133  INFO 15640 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
    2020-05-12 14:37:13.139  INFO 15640 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:mysql://172.31.9.185:3306/test (MySQL 5.6)
    2020-05-12 14:37:13.237  INFO 15640 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.019s)
    2020-05-12 14:37:13.251  INFO 15640 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema `test`: 1
    2020-05-12 14:37:13.253  INFO 15640 --- [           main] o.f.core.internal.command.DbMigrate      : Schema `test` is up to date. No migration necessary.
    2020-05-12 14:37:13.444  INFO 15640 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
    2020-05-12 14:37:13.556  INFO 15640 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-05-12 14:37:13.563  INFO 15640 --- [           main] com.boot.demo.DemoApplication            : Started DemoApplication in 7.369 seconds (JVM running for 7.908)
    execution @PreDestroy()
    execution DisposableBean.destroy()
    execution @Bean.destroyMethod()
    
    
  5. 安全退出方式
    windows在cmd中启动,ctrl+c方式安全退出。
    linux上启动程序,kill pid安全退出。
    idea中,找到如下图标点击,安全退出。
    SpringBoot 测试Bean生命周期_第2张图片

你可能感兴趣的:(SpringBoot,Spring)