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

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

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

09805a195b7e15d43ecaca0b01b20348.png

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

a5a0ba195dc910e04ef15fc81752e6c2.png

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

ApplicationStartingEvent

ApplicationEnvironmentPreparedEvent

ApplicationPreparedEvent

ApplicationStartedEvent

ApplicationReadyEvent

ApplicationFailedEvent

实现CommandLineRunner和ApplicationRunner完成启动加载数据

02afb8724283f2c24153b3146496e0c3.png

1a67f0f87ee118e1a8aef07278bb7c0e.png

二:关闭时完成某些操作

实现ApplicationListener

实现DisposableBean接口

0cda0c86a51ab5aa891ad42deb587061.png

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

A、非安全验证

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

org.springframework.boot

spring-boot-starter-actuator

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

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

3、关闭命令:

curl -X POST host:port/shutdown

B、安全验证

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

org.springframework.boot

spring-boot-starter-security

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

#开启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、关闭命令:

curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值