springboot關(guān)于容器啟動(dòng)事件總結(jié)
在springboot 容器啟動(dòng)時(shí),我們需要在啟動(dòng)過(guò)程中做一些操作,比如啟動(dòng)容器后,執(zhí)行某些代碼。
spring 提供了監(jiān)聽(tīng)器,我們可以方便的實(shí)現(xiàn)這些操作。
在容器啟動(dòng)開(kāi)始時(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ù)庫(kù)并把得分寫(xiě)入數(shù)據(jù)庫(kù)
這篇文章給大家分享了Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)中連接數(shù)據(jù)庫(kù)并把得分寫(xiě)入數(shù)據(jù)庫(kù)的相關(guān)知識(shí)點(diǎn)和代碼,有興趣的可以學(xué)習(xí)參考下。2018-07-07
Spring BeanPostProcessor源碼示例解析
這篇文章主要為大家介紹了Spring BeanPostProcessor源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
8種常見(jiàn)的接口請(qǐng)求重試方法總結(jié)
在跨境業(yè)務(wù)中,可能第三方的服務(wù)器分布在世界的各個(gè)角落,所以請(qǐng)求三方接口的時(shí)候,難免會(huì)遇到一些網(wǎng)絡(luò)問(wèn)題,這時(shí)候需要加入重試機(jī)制了,下面小編就給大家分享幾個(gè)接口重試的寫(xiě)法吧2023-11-11
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。接下來(lái)通過(guò)本文給大家介紹Java中的FileInputStream 和 FileOutputStream,需要的朋友可以參考下2017-05-05
SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例
這篇文章主要介紹了SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Mybatis-Plus接口BaseMapper與Services使用詳解
這篇文章主要為大家介紹了Mybatis-Plus接口BaseMapper與Services使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

