Spring Boot 启动载入数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去载入一些数据或做一些事情这种需求。
为了解决这种问题。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的更多相关文章
- (23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
[Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...
- spring boot启动加载项CommandLineRunner
spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...
- Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动
之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...
- Spring Boot启动过程(七):Connector初始化
Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...
- Spring Boot 返回 XML 数据,一分钟搞定!
Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击查看之前分享的 Spring Boot 返回 JSON 数据,一分钟搞定! ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- [Spring Boot] Spring Boot启动过程源码分析
关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...
- Spring Boot启动过程(三)
我已经很精简了,两篇(Spring Boot启动过程(一).pring Boot启动过程(二))依然没写完,接着来. refreshContext之后的方法是afterRefresh,这名字起的真.. ...
- Spring Boot启动原理解析
Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...
随机推荐
- Docker存出载入镜像
镜像的存出和载入 如果你的生产环境不能连通互联网,而你又希望从互联网上获取镜像.你就需要借助 docker save命令,可以将镜像导出为 tar 文件.使用 docker load 命令,可以将ta ...
- AppServ与IIS快速共存搭建PHP环境
一:AppServ 一路安装 其中,不能与IIS端口冲突,比如可以指定端口为8080: 安装完毕后验证 http://localhost:8080,验证 MySql是否能够打开: 二:IIS整合 新建 ...
- Java反编译工具Jad详解
做项目过程中需要反编译一个jar包,于是作了一些学习,记录下来. Jad(JAva Decompiler)是一个Java的反编译器,可以通过命令行把Java的class文件反编译成源代码. 如果你在使 ...
- diff详解
作者: 阮一峰 日期: 2012年8月29日 diff是Unix系统的一个很重要的工具程序. 它用来比较两个文本文件的差异,是代码版本管理的基石之一.你在命令行下,输入: $ diff <变动前 ...
- [转]PHP利用Gearman来处理并行多进程问题
From : http://www.yuansir-web.com/2013/11/25/php%E5%88%A9%E7%94%A8gearman%E6%9D%A5%E5%A4%84%E7%90%86 ...
- 看看Spring的源码(一)——Bean加载过程
首先Web项目使用Spring是通过在web.xml里面配置org.springframework.web.context.ContextLoaderListener初始化IOC容器的. <li ...
- N体运动的程序模拟
这依然是与<三体>有关的一篇文章.空间中三个星体在万有引力作用下的运动被称之为三体问题,参见我的上一篇文章:三体运动的程序模拟.而这一节,对三体问题进行了扩展,实现了空间中N个星体在万有引 ...
- how to fix bug in daily work
0 QE will begin test the product when system is stable. so they may log a lot of issues, and our dai ...
- 揭秘uc浏览器一
首先,看一下项目完成后的,最终效果是这样的: 一.主界面 二,书签界面 三.主界面 四.操作对话框界面 这几个界面你是否看到了uc浏览器的影子了,其实我说你也可以了,在接下来篇幅中,我将手把手叫大家完 ...
- mono touch登录设计
需要对MonoTouch.Dialog-1进行引用: using System; using System.Collections.Generic; using System.Linq; using ...