转:http://blog.csdn.net/linxingliang/article/details/52069503

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

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

package com.kfit.runner;

import org.springframework.boot.CommandLineRunner;

import org.springframework.stereotype.Component;

/**

* 服务启动执行

*

* @author   Angel(QQ:412887952)

*/

@Component

public class MyStartupRunner1implements CommandLineRunner {

@Override

publicvoid run(String...args) throws Exception {

System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");

}

}

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

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

package com.kfit.runner;

import org.springframework.boot.CommandLineRunner;

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

/**

* 服务启动执行

*

* @author   Angel(QQ:412887952)

*/

@Component

@Order(value=2)

publicclassMyStartupRunner1implementsCommandLineRunner {

@Override

publicvoid run(String...args) throws Exception {

System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111<<<<<<<<<<<<<");

}

}

}

package com.kfit.runner;

import org.springframework.boot.CommandLineRunner;

import org.springframework.core.annotation.Order;

import org.springframework.stereotype.Component;

/**

* 服务启动执行

*

* @author   Angel(QQ:412887952)

*/

@Component

@Order(value=1)

publicclassMyStartupRunner2implementsCommandLineRunner {

@Override

publicvoid run(String...args) throws Exception {

System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222<<<<<<<<<<<<<");

}

}

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

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

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

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

@Override

publicvoid run(String... args) throws Exception {

System.out.println(Arrays.asList(args));

System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作11111111<<<<<<<<<<<<<");

}

这里的args就是程序启动的时候进行设置的:

SpringApplication.run(App.class, new String[]{"hello,","林峰"});

这里为了做演示,配置为固定值了,其实直接接收main中的args即可,那么在运行的时候,进行配置即可。

 

题外话:

eclipse中给Java应用传args参数的方法如下:
1、先写好Java代码,比如文件名为IntArrqy.java;
2、在工具栏或菜单上点run as下边有个RunConfiguration;
3、在弹出窗口点选第二个标签arguments;
4、把你想输入的参数写在program argumenst就可以了,多个参数使用空格隔开。
完成后点run即可通过运行结果看到参数使用情况了。

23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】的更多相关文章

  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 来 ...

  3. 十三、 Spring Boot 启动加载数据 CommandLineRunner

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

  4. 7. Spring Boot 启动加载数据 CommandLineRunner

    转自:https://blog.csdn.net/catoop/article/details/50501710

  5. spring boot启动加载项CommandLineRunner

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

  6. spring boot启动加载数据

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

  7. 1.spring boot起步之Hello World【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  8. SpringBoot(七)-- 启动加载数据

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

  9. (2)Spring Boot返回json数据【从零开始学Spring Boot】

    在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web 然后新建一个类HelloControoler, 然后修改App.java类,主要是的这个类就是 ...

随机推荐

  1. maven国内镜像

    <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...

  2. [ZJOI2014][bzoj3527]力 [FFT]

    题面 传送门 思路 把要求的公式列出来: $E_i=\frac{F_i}{q_i}=\sum_{j=1}^i\frac{q_j}{\left(i-j\right)^2}-\sum_{j=i+1}^n\ ...

  3. [NOI2012][bzoj2879] 美食节 [费用流+动态加边]

    题面 传送门 思路 先看看这道题 修车 仔细理解一下,这两道题是不是一样的? 这道题的不同之处 但是有一个区别:本题中每一种车有多个需求,但是这个好办,连边的时候容量涨成$p\lbrack i\rbr ...

  4. JavaScript 笔记(3) -- JSON

    JavaScript JSON (JavaScript Object Notation): 是一种轻量级的数据交换格式 JSON 是用于存储和传输数据的格式 JSON 通常用于服务端向网页传递数据 J ...

  5. POJ1385 Lifting the Stone

    There are many secret openings in the floor which are covered by a big heavy stone. When the stone i ...

  6. Struts2的使用注解配置Action(零配置)

    1.首先引入struts2注解的jar包:struts2-convention-plugin.jar ------------------------------第一种方式-------------- ...

  7. 了解Binder机制原理和底层实现

    参考:http://www.2cto.com/kf/201606/515548.html 1.Binder通信机制介绍 这篇文章会先对比Binder机制与Linux的通信机制的差别,了解为什么Andr ...

  8. pylint & jenkins

    利用pylint 检测 python源码. Pylint 提供了简单的方式来分析 Python 代码,其高可配置性很容易使一个部门的人员使用统一的代码风格. Pylint 是什么 Pylint 是一个 ...

  9. Codeforces 707C. Pythagorean Triples-推公式的数学题

    两道C题题解,能推出来公式简直是无敌. http://codeforces.com/problemset/problem/707/C codeforces707C. Pythagorean Tripl ...

  10. extern "C"解析

    转自大牛的解析(非常具体详细)http://www.cnblogs.com/skynet/archive/2010/07/10/1774964.html 我做个简单的标注方便以后自己查看: 在用C++ ...