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 ...
随机推荐
- 用GDI+DrawImage画上去的图片会变大
问题: 用GDI+DrawImage画上去的图片会变大 解释: Status DrawImage(Image *image,const Point &point);两参数的这个接口是这么设计的 ...
- 异步加载js文件的方法总结
方法一,jQuery.getScript HTML 代码: 代码如下 复制代码 <button id="go">Run</button><div cl ...
- 变量命名神器Codelf
个人感觉,当觉得命名困难的时候,其实是因为还没有想清楚这个变量.这个方法或者这个类是要干什么,还不能用一个或几个词准确描述它的工作,才觉得无法命名,这是命名的最困难的阶段.而只要想清楚了它的任务,命名 ...
- hdu 4223 Dynamic Programming? (dp)
//连续的和的绝对值最小 # include <stdio.h> # include <string.h> # include <algorithm> # incl ...
- 编写 T4 文本模板
文本模板由以下部件组成: 1)指令 - 控制模板处理方式的元素. 2)文本块 - 直接复制到输出的内容. 3)控制块 - 向文本插入可变值并控制文本的条件或重复部件的程序代码. 指令: 指令是控制模板 ...
- 【转载】Spring Cache介绍
原文地址:http://www.cnblogs.com/rollenholt/p/4202631.html 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个 ...
- java中使用for遍历集合是注意的空指针异常
public static void main(String[] args) { List<Object> a = null; for(Object i : a)//会有空指针异常 { } ...
- Mysql show processlist 排查问题
一.命令概述: mysql show full processlist 用来查看当前线程处理情况,具体信息请参考官网:https://dev.mysql.com/doc/refman/5.7/en/s ...
- JDK1.7新特性,语言篇
1. 可以用二进制表达数字 可以用二进制表达数字(加前缀0b/0B),包括:byte, short, int, long // 可以用二进制表达数字(加前缀0b/0B),包括:byte, short, ...
- eclipse CDT写c++使用文件作为输入源(输入重定向)
在main函数第一句添加下面. freopen("inputfile","r",stdin); 创建一个inputfile,放project根文件夹下. 注意添 ...