码迷,mamicode.com
首页 > 编程语言 > 详细

【Spring Boot】Spring Boot之三种容器启动后进行相关应用初始化操作方法

时间:2019-11-03 22:09:31      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:anti   ati   als   llb   err   event   sys   str   tin   

一、方式一,使用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;
    }
}

 

【Spring Boot】Spring Boot之三种容器启动后进行相关应用初始化操作方法

标签:anti   ati   als   llb   err   event   sys   str   tin   

原文地址:https://www.cnblogs.com/756623607-zhang/p/11789199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!