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 ...
随机推荐
- Android面试之HashMap的实现原理
1.HashMap与HashTable的区别 HashMap允许key和value为null: HashMap是非同步的,线程不安全,也可以通过Collections.synchronizedMap( ...
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
In this post,I will show you SharePoint 2013 How to Backup Farm Automatically with a PowerShell and ...
- [转]PostgreSQL源码结构
PostgreSQL采用C/S(客户机/服务器)模式结构.应用层通过INET或者Unix Socket利用既定的协议与数据库服务器进行通信. 另外,还有一种‘Standalone Backend’使用 ...
- Android开发中的神坑和知识点记录
1.SDK Manager.exe闪退的问题 http://blog.csdn.net/fambit025/article/details/26984345 1.找到android.bat,在源码处找 ...
- Android Logcat信息级别解读
Android Logcat信息级别解读 Logcat信息分为好几个级别,分别是:Assert(断言).Debug(调试).Error(错误).Info(信息).Verbose(详细).Warning ...
- maven依赖包冲突解决办法
今天在写一个demo时报了以下错误 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding /slf4j-lo ...
- django -- model中只有Field类型的数据才能成为数据库中的列
一.model的定义: from django.db import models # Create your models here. class Person(models.Model): firs ...
- unity, ios skin crash
https://issuetracker.unity3d.com/issues/ios-loading-models-with-tangents-set-to-calculate-legacy-fro ...
- unity, Gizmos.DrawMesh一个坑
错误写法(画不出来): void OnDrawGizmos(){ Mesh mesh=new Mesh(); mesh.vertices=... mesh.triangles=... //mesh.R ...
- Windows系统创建符号链接文件
源文件夹:E:\深海 创建新硬链接文件夹:D:\微云同步盘\719179409\4-工作资料\深海 使用快捷键Win + X 打开以下菜单,选择命令提示符(管理员) 敲入以下命令: 创建成功后 ...