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

为了解决这种问题。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. TextView 中文文档

    属性名称 描述 android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all) ...

  2. [Android Security] jar文件转smali文件

    cp : https://blog.csdn.net/fengmm521/article/details/78446486 jar转smali文件一共要走两步,先将jar文件转为.dex文件 (dx工 ...

  3. Chocolatey 简介(软件自动化管理工具)

    一.Chocolatey 管理Windows软件的明智方法 1.建立在技术的无人值守安装和PowerShell.建立在技术的无人值守安装和PowerShell. 2.轻松管理Windows软件的所有方 ...

  4. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  5. 京东的Netty实践,京麦TCP网关长连接容器架构

    背景 早期京麦搭建 HTTP 和 TCP 长连接功能主要用于消息通知的推送,并未应用于 API 网关.随着逐步对 NIO 的深入学习和对 Netty 框架的了解,以及对系统通信稳定能力越来越高的要求, ...

  6. [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number

    1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...

  7. 【Eclipse】Eclipse-Build-缓慢-卡住

    Eclipse-Build-缓慢-卡住 eclipse building workspace 卡主_百度搜索 解决building workplace 导致的卡死,使得eclipse加速 - CSDN ...

  8. 使用TensorFlow动手实现一个Char-RNN

    https://blog.csdn.net/thriving_fcl/article/details/72565455 前言 学习RNN的时候很多人应该都有看过Andrej Karpathy写的The ...

  9. 文本分类需要CNN?No!fastText完美解决你的需求(前篇)

    http://blog.csdn.net/weixin_36604953/article/details/78195462?locationNum=8&fps=1 文本分类需要CNN?No!f ...

  10. 斯坦福大学CS224d课程目录

    https://www.zybuluo.com/hanxiaoyang/note/404582 Lecture 1:自然语言入门与次嵌入 1.1 Intro to NLP and Deep Learn ...