如果你需要在你的SpringBoot启动完成之后实现一些功能,那么可以通过创建class实现ApplicationRunner和CommandLineRunner来完成:

@Component
public class ApplicationRunnerTest implements ApplicationRunner { @Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("========>> ApplicationRunner.run");
for (String sourceArg : args.getSourceArgs()) {
System.out.println(sourceArg);
}
for (String optionName : args.getOptionNames()) {
System.out.println(optionName + " = " + args.getOptionValues(optionName));
}
System.out.println("========>> End");
}
}
@Component
public class CommandLineRunnerTest implements CommandLineRunner { @Override
public void run(String... args) throws Exception {
System.out.println("========>> CommandLineRunner.run");
for (String sourceArg : args) {
System.out.println(sourceArg);
}
System.out.println("========>> End");
}
}

如果你定义了多个ApplicationRunner或者CommandLineRunner,并想要控制他们执行的先后顺序,可以让你定义的class实现org.springframework.core.Ordered接口,或者直接注解@Order

SpringBoot的ApplicationRunner和CommandLineRunner的更多相关文章

  1. 剖析ApplicationRunner、CommandLineRunner

    需求:SpringBoot项目启动成功后执行某方法 方案:在spring-boot中提供了两种Runner接口:ApplicationRunner和CommandLineRunner,编写自己的类实现 ...

  2. SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展

    CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口: public interface CommandLineRunner { /** ...

  3. SpringBoot的ApplicationRunner

    Article1 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为Co ...

  4. SpringBoot之ApplicationRunner接口和@Order注解

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...

  5. springboot应用中使用CommandLineRunner

    在springboot应用中,存在这样的使用场景,在springboot ioc容器创建好之后根据业务需求先执行一些操作,springboot提供了两个接口可以实现该功能: CommandLineRu ...

  6. SpringBoot-容器启动的时候执行一些内容

    SpringBoot的ApplicationRunner.CommandLineRunner 场景: 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的. ...

  7. Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner

    本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...

  8. 使用CommandLineRunner或ApplicationRunner接口创建bean

    在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...

  9. Spring Boot 启动加载数据 CommandLineRunner

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...

随机推荐

  1. GoJS组织结构图2

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. c++中比较好用的“黑科技”

    切入正题,上黑科技 一.黑科技函数(常用的我就不写了,例如sort函数) 1.next_permutation(a+1,a+1+n) a[1-n]全排列 2.reverse(a+1,a+1+n) 将a ...

  3. centos7下安装JDK1.8

    步骤1:Oracle下载jdk-8u141-linux-x64安装包 步骤2:解压jdk-8u141-linux-x64.tar.gz到home目录 [root@model ~]# tar -zxvf ...

  4. 前端学习笔记系列一:8 <noscript>…</noscript>,网站路径,vscode跳出右括号

    1.<noscript>…</noscript> 在body中使用此段代码,可识别 <script> 标签但无法支持其中的脚本的浏览器. 此段代码意思为如果浏览器不 ...

  5. wumii 爆款总结经验

    在正式创办无秘之前,我们反思前几次创业失败的教训,深刻领悟两点: 第一,内容推荐的精准度取决于平台收集用户数据的能力,如果没有用户行为数据,产品无法做内容推荐,而通过简单的新闻排序,延长用户浏览单篇文 ...

  6. Lesson 48 Planning a share portfolio

    How does the older investor differ in his approach to investment from the younger investor? There is ...

  7. Myeclipse项目出现红叉解决方案

    1.右键点击你的项目.选中properties 2.选中MyEclipse下的Project Facets里面的java 此时的版本号为1.5,修改 3.选中MyEclipse下的Project Fa ...

  8. 第十届蓝桥杯 RSA解密(C++/ java)

    一道不错的题目,借鉴了网上的代码,用了拓展欧几里得算法求逆元,再用快速乘求每次a的余数,再快速幂对余数进行幂运算. #include <bits/stdc++.h> using names ...

  9. 记一次Redis+Getshell经验分享

    前言: 当我们接到一个授权渗透测试的时候,常规漏洞如注入.文件上传等尝试无果后,扫描端口可能会发现意外收获. 知己知彼乃百战不殆,Redis介绍: 简单来说 redis 就是一个Key-Value类型 ...

  10. docker学习笔记-04:docker容器数据卷

    一.容器数据卷是什么 1.为了保存docker容器运行时产生的数据,做数据的持久化,我们需要用到容器数据卷.因为如果不通过docker commit 生成新的镜像,那么当容器被删除时,数据自然就没有了 ...