实际应用中,我们会有在项目服务启动的时候就去载入一些数据或做一些事情这种需求。

为了解决这种问题。Spring Boot 为我们提供了一个方法。通过实现接口 CommandLineRunner 来实现。

非常easy。仅仅须要一个类就能够,无需其它配置。

创建实现接口 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 { @Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动运行,运行载入数据等操作<<<<<<<<<<<<<");
} }

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 { @Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动运行。运行载入数据等操作 11111111 <<<<<<<<<<<<<");
} }
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 { @Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动运行,运行载入数据等操作 22222222 <<<<<<<<<<<<<");
} }

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

>>>>>>>>>>>>>>>服务启动运行,运行载入数据等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动运行。运行载入数据等操作 11111111 <<<<<<<<<<<<<

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

Spring Boot 启动载入数据 CommandLineRunner的更多相关文章

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

    [Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...

  2. spring boot启动加载项CommandLineRunner

    spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...

  3. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  4. Spring Boot启动过程(七):Connector初始化

    Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...

  5. Spring Boot 返回 XML 数据,一分钟搞定!

    Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击查看之前分享的 Spring Boot 返回 JSON 数据,一分钟搞定! ...

  6. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  7. [Spring Boot] Spring Boot启动过程源码分析

    关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...

  8. Spring Boot启动过程(三)

    我已经很精简了,两篇(Spring Boot启动过程(一).pring Boot启动过程(二))依然没写完,接着来. refreshContext之后的方法是afterRefresh,这名字起的真.. ...

  9. Spring Boot启动原理解析

    Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...

随机推荐

  1. 标准输出中stderr和stdout的区别

    一.首先介绍一下三者printf,sprintf,fprintf的功能 1,printf就是标准输出,在屏幕上打印出一段字符串来. 2,sprintf就是把格式化的数据写入到某个字符串中.返回值字符串 ...

  2. 数学图形(1.47)贝塞尔(Bézier)曲线

    贝塞尔曲线又称贝兹曲线或贝济埃曲线,是由法国数学家Pierre Bézier所发现,由此为计算机矢量图形学奠定了基础.它的主要意义在于无论是直线或曲线都能在数学上予以描述. 上一节讲的是高次方程曲线, ...

  3. qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""

    转载:https://forum.qt.io/topic/97484/qt-5-12-make-mingw-32-release/12 I build Qt 5.12 with MinGW 7.3.0 ...

  4. SD内存卡禁止写入只读怎么办?另类SPI模式修复坏卡

    这几天很郁闷,买的32G TF卡老出问题,三星的标,不知道是不是正品,标称C10,且不说是不是真的三星和真的C10,反正测试一下容量倒是真的,价格么也比较亲民,好像是69块?   哎,可惜呀,好景不长 ...

  5. scrapy框架系列 (1) 初识scrapy

    Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...

  6. win8 中如何删除 共享文件夹 用户名和密码

    在访问共享文件夹时我们都喜欢选中记住用户名和密码,可是有时候密码输入错误或者密码修改了,这时就需要我们删除或则修改先前记住的用户名和密码记录. 首先进入:控制面板\所有控制面板项\凭据管理器 选择wi ...

  7. MFC中onmouseover与onmousemove的区别

    onmouseover与onmousemove的区别是:当鼠标移过当前对象时就产生了onmouseover事件,当鼠标在当前对象上移动时就产生了onmousemove事件,只要是在对象上移动而且没有移 ...

  8. 重装linuxserver简易流程

             项目开发软件开发非常重要的一个环节,而能够拥有一个安全健康的server来使System正常高效的执行也是非常有必要的.由于是搭建在外网上的server.时不时会受到各种病毒的侵袭, ...

  9. 如何感性地理解EM算法?

    https://www.jianshu.com/p/1121509ac1dc 如果使用基于最大似然估计的模型,模型中存在隐变量,就要用EM算法做参数估计.个人认为,理解EM算法背后的idea,远比看懂 ...

  10. js的技巧

    字典: if(tac["detail"][sid] || tac["detail"][sid]==0) //判断某项是否存在,0为真 tac["det ...