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

很简单,只需要一个类就可以,无需其他配置。 
创建实现接口 CommandLineRunner 的类

package org.springboot.sample.runner;import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;/*** 服务启动执行** @author   单红宇(365384722)* @myblog  http://blog.csdn.net/catoop/* @create    2016年1月9日*/
@Component
public class MyStartupRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Spring Boot应用程序在启动后,会遍历CommandLineRunner接口的实例并运行它们的run方法。也可以利用@Order注解(或者实现Order接口)来规定所有CommandLineRunner实例的运行顺序。

如下我们使用@Order 注解来定义执行顺序。

package org.springboot.sample.runner;import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;/*** 服务启动执行** @author   单红宇(365384722)* @myblog  http://blog.csdn.net/catoop/* @create    2016年1月9日*/
@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<");}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
package org.springboot.sample.runner;import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;/*** 服务启动执行** @author   单红宇(365384722)* @myblog  http://blog.csdn.net/catoop/* @create    2016年1月9日*/
@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<");}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

启动程序后,控制台输出结果为:

>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<
  • 1
  • 2

根据控制台结果可判断,@Order 注解的执行优先级是按value值从小到大顺序。

Spring Boot 启动加载数据 CommandLineRunner相关推荐

  1. 23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】

    2019独角兽企业重金招聘Python工程师标准>>> 102. Spring Boot之CommandLineRunner和ApplicationRunner[从零开始学Sprin ...

  2. springboot 启动加载数据 commandLineRunner

    项目启动时,我们需要加载一些数据或启动定时任务执行数据下载或同步,此时可以用到commandLineRunner类. @Component //被spring容器管理 @Order(value = 2 ...

  3. 在Spring Boot中加载初始化数据

    文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...

  4. spring 启动加载数据_12个很棒的Spring数据教程来启动您的数据项目

    spring 启动加载数据 Spring Data的任务是为数据访问提供一个熟悉且一致的,基于Spring的编程模型,同时仍保留基础数据存储的特​​殊特征. 它使使用数据访问技术,关系和非关系数据库, ...

  5. springboot 读取配置文件_使用 @ConfigurationProperties 在 Spring Boot 中加载配置

    本文地址: 使用 @ConfigurationProperties 在 Spring Boot 中加载配置 使用 Spring Boot 加载配置文件的配置非常便利,我们只需要使用一些注解配置一下就能 ...

  6. Spring Boot配置加载顺序

    Spring Boot 不仅可以通过配置文件进行配置,还可以通过环境变量.命令行参数等多种形式进行配置.这些配置都可以让开发人员在不修改任何代码的前提下,直接将一套 Spring Boot 应用程序在 ...

  7. Spring Boot : 资源加载器

    1.美图 2.概述 前言参考: 源码:Spring boot 主程序的功能(启动流程) ResourceLoader接口,在 Spring 中用于加载资源,通过它可以获取一个Resouce 对象.使用 ...

  8. Spring Boot 配置加载顺序详解

    使用 Spring Boot 会涉及到各种各样的配置,如开发.测试.线上就至少 3 套配置信息了.Spring Boot 可以轻松的帮助我们使用相同的代码就能使开发.测试.线上环境使用不同的配置. 在 ...

  9. Spring boot配置文件加载位置

    配置文件加载位置 springboot 启动 会扫描以下位置的application.properties 或者application.yml文件,作为Spring boot的默认配置文件 –file ...

最新文章

  1. python和前端之HTML的激情
  2. VS013的单元测试去哪里了
  3. 荣耀mgaic2鸿蒙系统,华为没有抛弃荣耀!我看着当年4400买的荣耀Magic2,不争气地哭了...
  4. Linux 多线程(二)线程安全:线程安全、互斥与互斥锁、死锁、同步与条件变量
  5. Spring Boot –如何跳过缓存thyemeleaf模板,js,css等以每次绕过重启服务器
  6. C++(STL):02---tuple容器
  7. 数据结构大作业_大数据课程笔记
  8. SQL:postgresql中判断一个点是否落在指定区域
  9. android 音频对比,差距只有安卓?索尼Zx300a与505全方位对比
  10. python_体脂率的计算
  11. win7安装PS2019CC启动时报d3dcompiler_47.dll的问题解决
  12. Mac解决Updating Homebrew卡顿
  13. python安装pip之后镜像源配置
  14. Windows下Eclipse for C/C++的“Launch failed. Binary not found”完美解决方案
  15. networking /etc/network/interfaces 笔记221102
  16. OneDrive登录失败解决办法
  17. mail在linux的端口,linux 上mailx通过465端口发送邮件
  18. 盛元广通医院实验室设备预约管理系统
  19. 热水比冷水结冰快,这就是所谓的姆潘巴现象
  20. MINIO使用说明(附文件上传下载)

热门文章

  1. 兼顾效率与安全:如何制止新模版注入漏洞?
  2. M4i—下一代高速数据采集、数字化仪平台
  3. 用PQ的Windows版完成分区的一些调整工作
  4. 深圳电信网速测试工具
  5. Simulink中From、Goto模块
  6. 关于fmincon和cvx
  7. 基于FFmpeg-4.0 SDK的PCM编码成AAC
  8. 区块链大热,和出版业如何发生关系?
  9. 通用SQL数据库查询语句精华使用简介
  10. PYQT中QThread输出到textBrowser