好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

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

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

applicationstartingevent applicationenvironmentpreparedevent applicationpreparedevent applicationstartedevent applicationreadyevent applicationfailedevent

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

二:关闭时完成某些操作

实现applicationlistener<contextclosedevent>

实现disposablebean接口 

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

查看更多关于详解Springboot应用启动以及关闭时完成某些操作的详细内容...

  阅读:17次