【Spring Boot 系列博客】

)前言【从零开始学Spring Boot】 :

http://412887952-qq-com.iteye.com/blog/2291496

)spring boot起步之Hello World【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2291500

)Spring Boot返回json数据【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blog/2291508

)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blogs/2292376

)Spring Boot普通类调用bean【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2292388

更多查看博客:http://412887952-qq-com.iteye.com/blog

实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。 
为了解决这样的问题,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

publicclassMyStartupRunner1implementsCommandLineRunner {

@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下边有个Run Configuration;
3、在弹出窗口点选第二个标签arguments;
4、把你想输入的参数写在program argumenst就可以了,多个参数使用空格隔开。
完成后点run即可通过运行结果看到参数使用情况了。

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

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

    转:http://blog.csdn.net/linxingliang/article/details/52069503 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求 ...

  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. Flume 读取JMS 消息队列消息,并将消息写入HDFS

    利用Apache Flume 读取JMS 消息队列消息.并将消息写入HDFS,flume agent配置例如以下: flume-agent.conf #name the  components on ...

  2. 使用 Swift 3.0 操控日期

    作者:Joe,原文链接,原文日期:2016-09-20译者:Cwift:校对:walkingway:定稿:CMB 当你在想要 大规模重命名 时,一个附带的挑战就是要确保所有相关的文档都必须同步更新.比 ...

  3. B1588 [HNOI2002]营业额统计 set||平衡树

    平衡树题,求每个点的前驱,照例可以用set水过...(平衡树还是不会写) 又新学了一个用法: set <int> ::iterator s1; 这样s1就可以直接附为set中的地址了.但是 ...

  4. [Apple开发者帐户帮助]一、开始(1)关于您的开发者帐户

    Apple开发人员网站提供了为Apple平台制作出色应用所需的工具和信息.如果您不熟悉Apple平台上的开发,可以免费使用.只需接受Apple开发者协议,即可为您创建一个帐户.使用此帐户下载测试版软件 ...

  5. 利用 Puppet 实现自动化管理配置 Linux 计算机集群

    随着虚拟化和云计算技术的兴起,计算机集群的自动化管理和配置成为了数据中心运维管理的热点.对于 IaaS.Paas.Saas 来说,随着业务需求的提升,后台计算机集群的数量也会线性增加.对于数据中心的运 ...

  6. A - High School: Become Human

    Problem description Year 2118. Androids are in mass production for decades now, and they do all the ...

  7. centos 修改ssh端口,以支持vsftp

    vi /etc/ssh/sshd_config Port 22 Port 2225执行/etc/init.d/sshd restart   启动SSH服务,这样SSH端口将同时工作与22和2225上. ...

  8. Jenkins上Git ssh登陆配置

    1. 首先登陆linux机器 2. 切换到jenkins用户 3. 生成ssh key  ssh-keygen -t rsa -C 'amysu@acxiom.com'   4. 将生成的ssh ke ...

  9. HBase编程 API入门系列之get(客户端而言)(2)

    心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. 前面是基础,如下 HBase编程 API入门系列之put(客户端而言)(1) package zhouls.bigdata.Hba ...

  10. Lambda&Linq

    var list = new List<Model> { , UserName = ", Email = "zhang3@yoy.com"}, , UserN ...