溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解Springboot應(yīng)用啟動以及關(guān)閉時(shí)完成某些操作

發(fā)布時(shí)間:2020-10-01 20:14:10 來源:腳本之家 閱讀:310 作者:Hakka_Stephen 欄目:編程語言

一:啟動時(shí)完成數(shù)據(jù)加載等需求

實(shí)現(xiàn)ApplicationListener接口,官方文檔截圖:

詳解Springboot應(yīng)用啟動以及關(guān)閉時(shí)完成某些操作

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

詳解Springboot應(yīng)用啟動以及關(guān)閉時(shí)完成某些操作

應(yīng)用監(jiān)聽器事件執(zhí)行先后順序如下:

  1. ApplicationStartingEvent
  2. ApplicationEnvironmentPreparedEvent
  3. ApplicationPreparedEvent
  4. ApplicationStartedEvent
  5. ApplicationReadyEvent
  6. ApplicationFailedEvent

實(shí)現(xiàn)CommandLineRunner和ApplicationRunner完成啟動加載數(shù)據(jù)

詳解Springboot應(yīng)用啟動以及關(guān)閉時(shí)完成某些操作

詳解Springboot應(yīng)用啟動以及關(guān)閉時(shí)完成某些操作

二:關(guān)閉時(shí)完成某些操作

實(shí)現(xiàn)ApplicationListener<ContextClosedEvent>

實(shí)現(xiàn)DisposableBean接口 

詳解Springboot應(yīng)用啟動以及關(guān)閉時(shí)完成某些操作

三、spring boot應(yīng)用關(guān)閉操作(Linux/unix/ubuntu環(huán)境下進(jìn)行)

A、非安全驗(yàn)證

1、項(xiàng)目pom.xml添加如下依賴包:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、application.properties文件添加如下內(nèi)容:

#啟用shutdownendpoints.shutdown.enabled=true#禁用密碼驗(yàn)證endpoints.shutdown.sensitive=false

3、關(guān)閉命令:

curl -X POST host:port/shutdown

B、安全驗(yàn)證

1、pom.xml添加如下依賴包:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、application.properties文件添加以下內(nèi)容:

#開啟shutdown的安全驗(yàn)證endpoints.shutdown.sensitive=true
#驗(yàn)證用戶名security.user.name=admin
#驗(yàn)證密碼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é)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI