服务器之家

服务器之家 > 正文

详解Springboot应用启动以及关闭时完成某些操作

时间:2021-06-10 14:08     来源/作者:Hakka_Stephen

一:启动时完成数据加载等需求

实现applicationlistener接口,官方文档截图:

详解Springboot应用启动以及关闭时完成某些操作

applicationlistener接口的泛型类可以使用applicationstartedevent和applicationreadyevent

详解Springboot应用启动以及关闭时完成某些操作

应用监听器事件执行先后顺序如下:

  1. applicationstartingevent
  2. applicationenvironmentpreparedevent
  3. applicationpreparedevent
  4. applicationstartedevent
  5. applicationreadyevent
  6. applicationfailedevent

实现commandlinerunner和applicationrunner完成启动加载数据

详解Springboot应用启动以及关闭时完成某些操作

详解Springboot应用启动以及关闭时完成某些操作

二:关闭时完成某些操作

实现applicationlistener<contextclosedevent>

实现disposablebean接口 

详解Springboot应用启动以及关闭时完成某些操作

三、spring boot应用关闭操作(linux/unix/ubuntu环境下进行)

a、非安全验证

1、项目pom.xml添加如下依赖包:

?
1
2
3
4
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-actuator</artifactid>
</dependency>

2、application.properties文件添加如下内容:

?
1
#启用shutdownendpoints.shutdown.enabled=true#禁用密码验证endpoints.shutdown.sensitive=false

3、关闭命令:

?
1
curl -x post host:port/shutdown

b、安全验证

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

?
1
2
3
4
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-security</artifactid>
</dependency>

2、application.properties文件添加以下内容:

?
1
2
3
4
5
6
#开启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、关闭命令:

?
1
curl -u admin:admin -x post http://127.0.0.1:8081/manage/shutdown

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_33281710/article/details/80689849

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部