(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类,主要是的这个类就是 ...
随机推荐
- [CTSC 2008] 祭祀
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1143 [算法] 答案为最小路径可重复点覆盖所包含的路径数,将原图G进行弗洛伊德传递闭 ...
- Arbitrage(floyd)
http://poj.org/problem?id=2240 #include <stdio.h> #include <string.h> <<; ][]; ][] ...
- BZOJ 1975 k短路 A*
思路: 直接上A* //By SiriusRen #include <queue> #include <cstdio> #include <cstring> #in ...
- POJ 1160 DP
题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...
- B - Alyona and mex(构造)
Problem description Alyona's mother wants to present an array of n non-negative integers to Alyona. ...
- 用 Django2.0 做 简单的BBS(前端用 Bootstrap)
实现目标: 开发首页显示BBS的标题和摘要,点击BBS的标题可跳转到BBS详细页面进行展示. 开发环境及开发工具: Python 3.6.3 Django 2.0 Pycharm 2017.3 实现过 ...
- 省市区县的sql语句——城市
/*SQLyog v10.2 MySQL - 5.5.48 : Database - 省市县****************************************************** ...
- Visual Studio UI Automation 学习(三)
昨天了解到UI Automation是微软的.Net Framework框架里的4个DLL文件,可以在Visual studio里写代码时引入引用和引用命名空间.然后去写自动化代码. 今天本来是跟着一 ...
- AI:IPPR的数学表示-CNN方法
前言: 随着超量类别PR和高精度的需求,人工特征方法局限性凸显出来,固定的特征hash压缩映射因其压缩损失.表现为特定的特征hash方法,在海量的同类数据集上近邻特性变差,而在不同类别的数据上面隔离性 ...
- 论 fmap、fmap fmap、与 fmap fmap fmap
https://blog.csdn.net/sinat_25226993/article/details/44415803