在服务启动完成时,如果需要执行一些特定的预加载任务,则可以通过实现 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 启动完成时执行指定任务的更多相关文章

  1. Spring Boot 启动(二) Environment 加载

    Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...

  2. [Spring Boot] Spring Boot启动过程源码分析

    关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...

  3. Spring Boot启动过程源码分析--转

    https://blog.csdn.net/dm_vincent/article/details/76735888 关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Sp ...

  4. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  5. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  6. Spring Boot 启动(一) SpringApplication 分析

    Spring Boot 启动(一) SpringApplication 分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html ...

  7. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  8. Spring Boot启动原理解析

    Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...

  9. spring boot启动加载项CommandLineRunner

    spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...

随机推荐

  1. Win7 安装 MongoDB

    MongoDB 下载 MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mon ...

  2. SharePoint中在线编辑文档

    我一直以为只有在Document Library里面的File才会支持在线编辑.直到今天早上我才发现用IE打开List里面的Attachments也是支持在线编辑的,但前提是必须是IE浏览器. 目前正 ...

  3. 坑爹的高德地图API

    症状 ld: '-[MASearch poiSearchWithOption:]' in *****/Release-iphonesimulator/libMASearchKit.a(MASearch ...

  4. 常用代码之七:静态htm如何包含header.htm和footer.htm。

    要实现这个有多种解决方案,比如asp, php, 服务器端技术,IFrame等,但本文所记录的仅限于用jQuery和纯htm的解决方案. <head> <title></ ...

  5. 基础005_V7-Select IO

    主要参考ug471.pdf.

  6. 稀疏傅里叶变换(sparse FFT)

    作者:桂. 时间:2018-01-06  14:00:25 链接:http://www.cnblogs.com/xingshansi/p/8214122.html 前言 对于数字接收来讲,射频域随着带 ...

  7. Android studio界面相关设置

    用惯了emacs的操作方式,每当使用一款新的编辑器的时候,第一个想到的就是这个工具有没有emacs的快捷键,Android studio也是一样的. 1. Android studio设置emacs的 ...

  8. 配置ssh的config文件-为每个ssh连接创建别名

    其实选来选去,还是iterm2就够用了,唯一比较麻烦的就是每次都有输入地址和用户名,其实,可以通过配置文件简写的. 可以在 ~/.ssh/config文件写上配置alias信息,以后ssh的时候根据a ...

  9. 使用ASIHTTPRequest和ASIDownloadCache实现本地缓存

    源码:http://files.cnblogs.com/ios8/ASIHttpRequestDemo2.zip NSURL *url = [NSURLURLWithString:@"htt ...

  10. vue-cli项目配置图解

    配置vue项目机构步骤1.搭建环境:node.npm2.安装vue脚手架工具:npm install -g vue-cli3.初始化项目:vue init webpack vuedemo(“vuede ...