SpringBoot监听程序的启动、停止事件的方法
2019-08-05 17:18
831 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_16660569/article/details/98500718
程序开发时,一般需要在程序启动后执行一段初始化的逻辑,在程序停止之前,执行一段“优雅终止”的逻辑。如下示例演示了在SpringBoot应用中,如何使程序在启动后或者停止前执行指定的逻辑。
1. pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>syb</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>testpid</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. 程序启动事件监听器,代码如下:
package syb.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; public class StartListener implements ApplicationListener<ContextRefreshedEvent> { private Logger logger = LoggerFactory.getLogger(getClass()); @Override public void onApplicationEvent(ContextRefreshedEvent event) { logger.info("程序启动"); } }
3. 程序停止事件监听器,代码如下:
package syb.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; public class CloseListener implements ApplicationListener<ContextClosedEvent> { private Logger logger = LoggerFactory.getLogger(getClass()); @Override public void onApplicationEvent(ContextClosedEvent event) { logger.info("程序停止"); } }
4. 启动引导类代码如下,此处需要将前面的两个监听器添加到SpingApplication对象中:
ge syb.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication sa = new SpringApplication(App.class); sa.addListeners(new StartListener()); sa.addListeners(new CloseListener()); sa.run(args); } }
5. 将程序打包,上传至linux系统,启动程序,可以看到打印“程序启动”,使用kill命令终止程序,可以看到打印“程序停止”。
相关文章推荐
- Centos下SpringBoot项目启动与停止脚本的方法
- Linux 启动停止SpringBoot jar 程序部署Shell 脚本的方法
- springboot 事件监听的实现方法
- 监听Spring Boot的启动、停止、重启、关闭
- Spring Boot 启动配置原理和启动事件监听
- ORACLE10G 监听程序启动后自动停止的最新解决方法
- 错误备忘_Oracle 监听无法启动“……某些服务在未由其他服务或程序使用时将自动停止”
- SpringBoot项目不占用端口启动的方法
- Android下 scrollview的滚动停止事件的监听方法
- Spring Boot启动端口修改方法
- Intellij IDEA实现SpringBoot项目多端口启动的两种方法
- Linux编辑启动停止重启springboot jar包脚本
- spring boot实战二 事件监听
- ORACLE“监听程序未启动或数据库服务未注册到该监听程序”解决方法
- springboot学习----事件监听
- 009-Spring Boot 事件监听、配置与方式
- oracle 监听程序无法启动解决方法。
- SpringBoot应用启动服务要执行一些初始化操作的可用方法@PostConstruct与CommandLineRunner、ApplicationRunner
- Spring Boot应用事件监听示例详解
- 程序的方法启动或停止指定的系统后台服务