1. 程式人生 > >spring boot 專案重新搭建----------定時任務、事件監聽

spring boot 專案重新搭建----------定時任務、事件監聽

1.開啟定時任務

.

[email protected]設定定時時間規則

3.有時候需要程式啟動就進行執行的操作可用事件監聽來實現

監聽ContextRefreshedEvent事件,當所有的bean都初始化完成並被成功裝載後會觸發該事件,實現ApplicationListener<ContextRefreshedEvent>介面可以收到監聽動作,然後可以寫自己的邏輯

@Component
public class DemoListenner implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null){
            System.out.println("程式啟動了");
        }
    }
}