在服务启动完成时,如果需要执行一些特定的预加载任务,则可以通过实现 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. SharePoint 2013 启用 以其他用户身份登陆(Sign in as different user)

    习惯于SharePoint 2010的用户会发现,SharePoint 2013默认把  以其他用户身份登陆(Sign in as different user)的选项去掉了,这对于开发人员来说很麻烦 ...

  2. GGSN与SGSN简介

    GPRS核心网是GPRS(general packet radio service)系统的核心部分,GPRS的作用在于传输IP包,广泛应用于2G的GSM和3G的WCDMA网络. 1.GPRS核心网基本 ...

  3. windows 中安装及使用 SSH Key

    转自 简书技术博客:https://www.jianshu.com/p/a3b4f61d4747 联系管理员开通ssh功能: 重新创建环境: 下载工具包到本地机器wsCli 0.4 解压后,把相应的w ...

  4. IDEA(2018.01)安装和破解

    IDEA(2018.01)安装和破解 1.下载IDE https://www.jetbrains.com/idea/download/#section=windows 选择Ultimate版本 2.下 ...

  5. 温故而知新 Ajax 的新坑 dataType: 'json'

    为了方便实验,我随便捏造了一个json数据,然后放在php中输出. 请求明明是200,json数据也正确,但ajax就是不执行success回调? 原因是 dataType: 'json', 导致的. ...

  6. 【Android】1.1 开发环境安装和配置

    分类:C#.Android.VS2015: 创建日期:2016-01-20 2016-08-03说明:此版本已过时,最新版本见本博客置顶的内容. 一.安装JDK.SDK.NDK 无论是用C#和VS20 ...

  7. UISwipeGestureRecognizer 左右事件捕捉

    转自:http://blog.163.com/china_uv/blog/static/117137267201252102612185/ UISwipeGestureRecognizer 左右事件相 ...

  8. javascript 关于局部变量和全局变量

    js中函数运行过程不仅仅是单纯的局部变量覆盖全局变量.和函数里面的声明情况有关. 比方: <script> var a =1; function test(){ alert(a); //a ...

  9. mongodb更新数组中的所有匹配项

    假如集合中有如下数据 { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : & ...

  10. .NET MVC5+ Dapper+扩展+微软Unity依赖注入实例

    1.dapper和dapper扩展需要在线安装或者引用DLL即可 使用nuget为项目增加Unity相关的包 2.model类 public class UserInfo { public int I ...