详解Springboot应⽤启动以及关闭时完成某些操作⼀:启动时完成数据加载等需求
实现ApplicationListener接⼝,官⽅⽂档截图:
ApplicationListener接⼝的泛型类可以使⽤ApplicationStartedEvent和ApplicationReadyEvent
应⽤事件执⾏先后顺序如下:
springframework依赖1. ApplicationStartingEvent
2. ApplicationEnvironmentPreparedEvent
3. ApplicationPreparedEvent
4. ApplicationStartedEvent
5. ApplicationReadyEvent
6. ApplicationFailedEvent
实现CommandLineRunner和ApplicationRunner完成启动加载数据
⼆:关闭时完成某些操作
实现ApplicationListener<ContextClosedEvent>
实现DisposableBean接⼝
三、spring boot应⽤关闭操作(Linux/unix/ubuntu环境下进⾏)
A、⾮安全验证
1、项⽬l添加如下依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、application.properties⽂件添加如下内容:
#启⽤abled=true#禁⽤密码验证endpoints.shutdown.sensitive=false 3、关闭命令:
curl -X POST host:port/shutdown
B、安全验证
1、l添加如下依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、application.properties⽂件添加以下内容:
#开启shutdown的安全验证endpoints.shutdown.sensitive=true
#验证⽤户名security.user.name=admin
#验证密码security.user.password=admin
#⾓⾊le=SUPERUSER
# 指定端⼝management.port=8081
# 指定地址management.address=127.0.0.1
3、关闭命令:
curl -u admin:admin -X POST 127.0.0.1:8081/manage/shutdown
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。