詳解Springboot應(yīng)用啟動以及關(guān)閉時完成某些操作
一:啟動時完成數(shù)據(jù)加載等需求
實現(xiàn)ApplicationListener接口,官方文檔截圖:

ApplicationListener接口的泛型類可以使用ApplicationStartedEvent和ApplicationReadyEvent

應(yīng)用監(jiān)聽器事件執(zhí)行先后順序如下:
- ApplicationStartingEvent
- ApplicationEnvironmentPreparedEvent
- ApplicationPreparedEvent
- ApplicationStartedEvent
- ApplicationReadyEvent
- ApplicationFailedEvent
實現(xiàn)CommandLineRunner和ApplicationRunner完成啟動加載數(shù)據(jù)


二:關(guān)閉時完成某些操作
實現(xiàn)ApplicationListener<ContextClosedEvent>
實現(xiàn)DisposableBean接口

三、spring boot應(yīng)用關(guān)閉操作(Linux/unix/ubuntu環(huán)境下進行)
A、非安全驗證
1、項目pom.xml添加如下依賴包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、application.properties文件添加如下內(nèi)容:
#啟用shutdownendpoints.shutdown.enabled=true#禁用密碼驗證endpoints.shutdown.sensitive=false
3、關(guān)閉命令:
curl -X POST host:port/shutdown
B、安全驗證
1、pom.xml添加如下依賴包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、application.properties文件添加以下內(nèi)容:
#開啟shutdown的安全驗證endpoints.shutdown.sensitive=true #驗證用戶名security.user.name=admin #驗證密碼security.user.password=admin #角色management.security.role=SUPERUSER # 指定端口management.port=8081 # 指定地址management.address=127.0.0.1
3、關(guān)閉命令:
curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java中阻塞隊列BlockingQueue的詳解
這篇文章主要介紹了關(guān)于Java中阻塞隊列BlockingQueue的詳解,BlockingQueue是為了解決多線程中數(shù)據(jù)高效安全傳輸而提出的,從阻塞這個詞可以看出,在某些情況下對阻塞隊列的訪問可能會造成阻塞,需要的朋友可以參考下2023-05-05
Spring Boot插件spring tool suite安裝及使用詳解
這篇文章主要介紹了Spring Boot插件spring tool suite安裝及使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-08-08
SpringMVC自定義攔截器登錄檢測功能的實現(xiàn)代碼
這篇文章主要介紹了SpringMVC自定義攔截器登錄檢測功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
java高級用法之綁定CPU的線程Thread?Affinity簡介
java線程thread affinity是用來將java代碼中的線程綁定到CPU特定的核上,用來提升程序運行的性能,這篇文章主要介紹了java高級用法之綁定CPU的線程thread affinity的相關(guān)知識,需要的朋友可以參考下2022-05-05

