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

网友投稿 378 2023-01-19


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

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

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

Applicatihttp://onListener接口的泛型类可以使用ApplicationStartedEvent和ApplicationReadyEvent

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

ApplicationStartingEvent

ApplicationEnvironmentPreparedEvent

ApplicationPreparedEvent

ApplicationStartedEvent

ApplicationReadyEvent

ApplicationFailedEvent

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

二:关闭时完成某些操作

实现ApplicationListener

实现DisposableBean接口

三、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

实现DisposableBean接口

三、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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot与React集成的示例代码
下一篇:Spring cloud踩坑记录之使用feignclient远程调用服务404的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~