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)文章
Spring?Boot整合log4j2日志配置的詳細(xì)教程
這篇文章主要介紹了SpringBoot項(xiàng)目中整合Log4j2日志框架的步驟和配置,包括常用日志框架的比較、配置參數(shù)介紹、Log4j2配置詳解以及使用步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
springboot2.5.2與 flowable6.6.0整合流程引擎應(yīng)用分析
這篇文章主要介紹了springboot2.5.2與 flowable6.6.0整合流程引擎應(yīng)用分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
SpringBoot使用Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限詳解流程
本文小編將基于?SpringBoot?集成?Shiro?實(shí)現(xiàn)動(dòng)態(tài)uri權(quán)限,由前端vue在頁面配置uri,Java后端動(dòng)態(tài)刷新權(quán)限,不用重啟項(xiàng)目,以及在頁面分配給用戶?角色?、?按鈕?、uri?權(quán)限后,后端動(dòng)態(tài)分配權(quán)限,用戶無需在頁面重新登錄才能獲取最新權(quán)限,一切權(quán)限動(dòng)態(tài)加載,靈活配置2022-07-07
Java的MyBatis框架中實(shí)現(xiàn)多表連接查詢和查詢結(jié)果分頁
這篇文章主要介紹了Java的MyBatis框架中實(shí)現(xiàn)多表連接查詢和查詢結(jié)果分頁,借助MyBatis框架中帶有的動(dòng)態(tài)SQL查詢功能可以比普通SQL查詢做到更多,需要的朋友可以參考下2016-04-04

