springboot關(guān)于容器啟動(dòng)事件總結(jié)
在springboot 容器啟動(dòng)時(shí),我們需要在啟動(dòng)過程中做一些操作,比如啟動(dòng)容器后,執(zhí)行某些代碼。
spring 提供了監(jiān)聽器,我們可以方便的實(shí)現(xiàn)這些操作。
在容器啟動(dòng)開始時(shí):
package com.neo.filter;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent arg0) {
System.err.println("ApplicationStartingEventListener");
}
}
在容器啟動(dòng)完成后執(zhí)行操作:
package com.neo.filter;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent>,Ordered {
@Override
public void onApplicationEvent(ApplicationStartedEvent ev) {
System.out.println("ApplicationStartedEventListener1");
}
@Override
public int getOrder() {
return 1;
}
}
如果需要有順序執(zhí)行,我們可以實(shí)現(xiàn)Ordered接口,只越小,越先執(zhí)行。
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.neo.filter.ApplicationStartedEventListener;
import com.neo.filter.ApplicationStartedEventListener2;
import com.neo.filter.ApplicationStartingEventListener;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app=new SpringApplication(DemoApplication.class);
app.addListeners(new ApplicationStartedEventListener());
app.addListeners(new ApplicationStartingEventListener());
app.addListeners(new ApplicationStartedEventListener2());
app.run(args);
}
}
以上就是關(guān)于springboot容器啟動(dòng)事件的相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫
這篇文章給大家分享了Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)中連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫的相關(guān)知識(shí)點(diǎn)和代碼,有興趣的可以學(xué)習(xí)參考下。2018-07-07
Spring BeanPostProcessor源碼示例解析
這篇文章主要為大家介紹了Spring BeanPostProcessor源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
spring profile 多環(huán)境配置管理詳解
這篇文章主要介紹了 spring profile 多環(huán)境配置管理詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01
Java中的FileInputStream 和 FileOutputStream 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
FileInputStream 是文件輸入流,它繼承于InputStream。FileOutputStream 是文件輸出流,它繼承于OutputStream。接下來通過本文給大家介紹Java中的FileInputStream 和 FileOutputStream,需要的朋友可以參考下2017-05-05
SSM項(xiàng)目中使用攔截器和過濾器的實(shí)現(xiàn)示例
這篇文章主要介紹了SSM項(xiàng)目中使用攔截器和過濾器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Mybatis-Plus接口BaseMapper與Services使用詳解
這篇文章主要為大家介紹了Mybatis-Plus接口BaseMapper與Services使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

