(23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
【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】的更多相关文章
- 23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069503 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求 ...
- Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- 十三、 Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- 7. Spring Boot 启动加载数据 CommandLineRunner
转自:https://blog.csdn.net/catoop/article/details/50501710
- spring boot启动加载项CommandLineRunner
spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...
- spring boot启动加载数据
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求.为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实 ...
- 1.spring boot起步之Hello World【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- SpringBoot(七)-- 启动加载数据
一.场景 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求.为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunn ...
- (2)Spring Boot返回json数据【从零开始学Spring Boot】
在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.kfit.test.web 然后新建一个类HelloControoler, 然后修改App.java类,主要是的这个类就是 ...
随机推荐
- java调用c++ dll出现中文乱码
近期的开发用到了使用java调用本机动态连接库的功能,将文件路径通过java调用C++代码对文件进行操作. 在调用中假设路径中包括有中文字符就会出现故障.程序执行就会中止. 以下用一个小样例,来说明记 ...
- Expression Trees (C# and Visual Basic)
https://msdn.microsoft.com/en-us/library/bb397951.aspx Expression trees represent code in a tree-lik ...
- B2002 [Hnoi2010]Bounce 弹飞绵羊 分块
原来做过,看大家都做这道题都热情高涨,沈爷爷debug这道题4天,作为告诉他这个题的人,我还有点不好意思...我自己也就做了一个小时. 其实这个题思路还好,就是维护每个点的出块次数和跳出块的位置,然后 ...
- LA6878
区间dp dp[i][j]存i->j区间的所有取值 然后枚举分割点,枚举两个存的值,分别运算存储. 看见这种不确定分割顺序,两个区间合并的情况,就要用区间dp. #include<bits ...
- PCB MS SQL表值函数与CLR 表值函数 (例:字符串分割转表)
将字符串分割为表表经常用到,这里 SQL表值函数与CLR 表值函数,两种实现方法例出来如下: SELECT * FROM FP_EMSDB_PUB.dbo.SqlSplit('/','1oz/1.5 ...
- log4net实用配置代码
log4net实用配置代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> ...
- asp.net限制了上传文件大小为..M,解决方法
asp.net限制了上传文件大小为4M,在:在web.config里加下面一句,加在<System.web></System.web>之间如下:<system.web&g ...
- 基于bootstarp的Dailog
oaoDailog开发帮助 1. 帮助文档关键字 boostrap模态框oaoDailog 2. 使用场景 当网页上点击某个按钮需要给予用户提示确认,用户点击确认按钮才 ...
- [原创]PM2使nodejs应用随系统启动
1.安装 npm install pm2 -gd 2.启动 pm2 start ./bin/www 3.pm2 save 4.pm2 startup 5.pm2 save 注:上面几个步骤有几个坑: ...
- 算法之dfs篇
dfs算法是深度搜索算法.从某一节点开始遍历直至到路径底部,如果不是所寻找的,则回溯到上个节点后,再遍历其他路径.不断重复这个过程.一般此过程消耗很大,需要一些优化才能保持算法的高效. hdu1010 ...