zoukankan      html  css  js  c++  java
  • 【Spring Boot】Spring Boot之五种容器启动后进行相关应用初始化操作方法

    一、方式一,使用ApplicationListener<E extends ApplicationEvent>监听ContextRefreshedEvent事件

    /**
     * @author zhangboqing
     * @date 2019-11-03
     */
    @Component
    public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
        @Autowired
        private MyService myService;
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            System.out.println(">>>>>>>>>>>>>> ApplicationListener:" + myService.sayHello());
        }
    }

    二、方式二,使用SmartInitializingSingleton

    /**
     * @author zhangboqing
     * @date 2019-11-03
     */
    @Component
    public class MySmartInitializingSingleton implements SmartInitializingSingleton {
        @Autowired
        private MyService myService;
    
        @Override
        public void afterSingletonsInstantiated() {
            System.out.println(">>>>>>>>>>>>>> SmartInitializingSingleton:" + myService.sayHello());
        }
    }

    三、方式三,使用SmartLifecycle

    /**
     * @author zhangboqing
     * @date 2019-11-03
     */
    @Component
    public class MySmartLifecycle implements SmartLifecycle {
    
        @Autowired
        private MyService myService;
    
        @Override
        public void start() {
            System.out.println(">>>>>>>>>>>>>> SmartLifecycle:" + myService.sayHello());
        }
    
        @Override
        public boolean isAutoStartup() {
            return true;
        }
    
        @Override
        public void stop(Runnable callback) {
    
        }
    
        @Override
        public void stop() {
    
        }
    
        @Override
        public boolean isRunning() {
            return false;
        }
    
        @Override
        public int getPhase() {
            return 0;
        }
    }

    四、方式四,使用ApplicationRunner

    /**
     * @author zhangboqing
     * @date 2019-11-07
     */
    @Component
    public class MyApplicationRunner implements ApplicationRunner {
        @Autowired
        private MyService myService;
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println(">>>>>>>>>>>>>> ApplicationRunner:" + myService.sayHello());
        }
    }

    五、方式五,使用CommandLineRunner

    /**
     * @author zhangboqing
     * @date 2019-11-07
     */
    @Component
    public class MyCommandLineRunner implements CommandLineRunner {
    
        @Autowired
        private MyService myService;
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println(">>>>>>>>>>>>>> CommandLineRunner:" + myService.sayHello());
        }
    }
  • 相关阅读:
    amazeui学习笔记--css(HTML元素1)--按钮Button
    【DataStructure】The description of Java Collections Framework
    android面试题 不仅仅是面试是一个很好的学习
    SVNclient安装与使用
    Microsoft Visual C++ Runtime Library Runtime Error解决的方式
    fullcalendar日历控件集合知识
    android存储阵列数据SharedPreferences
    Codeforces 484E Sign on Fence(是持久的段树+二分法)
    漂亮的表格样式(使用CSS样式表控制表格样式)
    交叉编译libxml2
  • 原文地址:https://www.cnblogs.com/756623607-zhang/p/11789199.html
Copyright © 2011-2022 走看看