Spring-boot 启动完成时执行指定任务
在服务启动完成时,如果需要执行一些特定的预加载任务,则可以通过实现 CommandLineRunner 接口来实现。
实现
@Component
public class Started implements CommandLineRunner{
private static final Logger LOGGER = LoggerFactory.getLogger(Started.class); @Override
public void run(String... strings) throws Exception {
LOGGER.info("App Starting ... ");
LOGGER.info("------------------------------------------------------------------------------");
LOGGER.info("| | |");
LOGGER.info("| --====|====-- |");
LOGGER.info("| | |");
LOGGER.info("| |");
LOGGER.info("| .-'''''-. |");
LOGGER.info("| .'_________'. |");
LOGGER.info("| /_/_|__|__|_\\_\\ |");
LOGGER.info("| ;'-._ _.-'; |");
LOGGER.info("| ,--------------------| `-. .-' |--------------------, |");
LOGGER.info("| ``''--..__ ___ ; ' ; ___ __..--''`` |");
LOGGER.info("| `'-// \\\\.._\\ /_..// \\\\-'` |");
LOGGER.info("| \\\\_// '._ _.' \\\\_// |");
LOGGER.info("| `-` ``---`` `-` |");
LOGGER.info("------------------------------------------------------------------------------");
}
}
效果:

如上,在应用中,可以通过加入明显日志的形式,提示是否发布成功。
可以看到,其输出在 StartupInfoLogger 之前。
优先级
如果存在多个 CommandLineRunner 实现类时,可以通过 @Order 来规定它们的加载顺序,如下所示:
@Component
@Order(1)
public class Started implements CommandLineRunner{
...
}
其中注解的 value 指运行的优先级,越小则越优先。
参考资料
[1] Spring Boot 启动加载数据 CommandLineRunner
Spring-boot 启动完成时执行指定任务的更多相关文章
- Spring Boot 启动(二) Environment 加载
Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...
- [Spring Boot] Spring Boot启动过程源码分析
关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...
- Spring Boot启动过程源码分析--转
https://blog.csdn.net/dm_vincent/article/details/76735888 关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Sp ...
- Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动
之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...
- Spring Boot 启动(二) 配置详解
Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...
- Spring Boot 启动(一) SpringApplication 分析
Spring Boot 启动(一) SpringApplication 分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- Spring Boot启动原理解析
Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...
- spring boot启动加载项CommandLineRunner
spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...
随机推荐
- MongoDB学习笔记(10)-- 排序
MongoDB sort() 方法 在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序 ...
- Unity3D对apk反编译、重编译、重签名
本文源链接:http://blog.csdn.net/qq393830887/article/details/56025923 工具 Java环境 Apktool(这玩意有些坑爹,不是官网最新的就好, ...
- http1.0 和 http1.1 主要区别
1.背景 KeepAlive是就是通常所称的长连接.KeepAlive带来的好处是可以减少tcp连接的开销,这对于短response body的请求效果更加明显.同时,可以为采用HTTP协议的交互 ...
- 基于TransactionScope类的分布式隐式事务
System.Transactions 命名空间中除了上一节中提到的基于 Transaction 类的显式编程模型,还提供使用 TransactionScope 类的隐式编程模型,它与显示编程模型相比 ...
- python .dcm文件读取,并转化为.jpg格式
.dcm文件是DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信中记录医学图像和相关信息的文件,在用于医学图像处理的时候我们 ...
- 7 款顶级的开源 Web 分析软件
Web 分析无非就是 Web 流量的测量.但它并不限于测量网络流量,还包括: 分析 数据采集 为了了解和优化网页而上报网络数据 Google Analytics是最广泛使用的基于云的网络分析服务.不过 ...
- ELK 中的elasticsearch 集群的部署
本文内容 背景 ES集群中第一个master节点 ES slave节点 本文总结 Elasticsearch(以下简称ES)搭建集群的经验.以 Elasticsearch-rtf-2.2.1 版本为例 ...
- Jackson(使用注解)
jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解 @JsonIgnoreProperties此注解是类注解,作用是json序列化时将Java bean ...
- struts2:OGNL表达式之#、%、$符号运用
1. OGNL表达达符号"#" 1.1 #用于访问OGNL上下文和Action上下文,#相当于ActionContext.getContext() 注意:当系统创建了Action实 ...
- Android 源码阅读笔记
Zygote: .Zygote 是系统启动之后创建的第二个进程2.Zygote 创建zygote Socket.虚拟机3.Zygote 在虚拟机中通过forkSystemServer, 创建Serve ...