23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
转: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】的更多相关文章
- (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 来 ...
- 十三、 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类,主要是的这个类就是 ...
随机推荐
- [oldboy-django][6其他]navicat远程登录没有权限
day6-17-1204 # 增加远程访问mysql的权限(就是其他ip地址远程访问另外一个ip地址的数据库) -- step1 修改配置文件,bind_address, 允许所有ip地址都可以访问m ...
- mysql数据库二进制初始化出现:170425 17:47:04 [ERROR] /application/mysql//bin/mysqld: unknown option '--skip-locking' 170425 17:47:04 [ERROR] Aborting 解决办法
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/application/mysql/ --data ...
- nodejs+express+mongodb搭建博客
https://github.com/lanleilin/sayHelloBlog 是可以运行的 https://github.com/lanleilin/sayHelloBlog 文件结构如下: c ...
- 【01】npm/cnpm安装
包安装相关信息: 1.node_modules文件夹 node_modules文件夹在nodejs中是一个特殊的文件夹,通过它的名字就可以看出,该文件夹也是用于存放node模块.如果一个模块表达式不是 ...
- 四个简单易用的demo,关于iOS定时器和延时的,非常好用。
1,延时执行(不可重复) 效果我直接截取控制台的日志了,就不做UI了. 2,用NSTimer执行定时和延时(可重复) [objc] view plain copy /** ** timer 可重复 * ...
- (三)Spring 依赖注入
一.Spring框架本身有四大原则: 使用POJO进行轻量级和最小侵入式开发. 通过依赖注入和接口变成实现松耦合. 通过AOP和默认习惯进行声明式变成. 使用AOP和模板减少模式化代码. Spring ...
- Cflow使用详解【转】
转自:http://blog.csdn.net/hanchaoqi/article/details/40922615 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近使用cflow,根据Cf ...
- android中与Adapter相关的控件----Spinner&AutoCompleteTextView
Spinner(列表选项框) & AutoCompleteTextView(自动完成文本框) 一.列表选项框核心属性 android:dropDownHorizontalOffset设置列表框 ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---16
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- eWebEditor不支持IE7以上版本Bug修改
修改: \Include\Editor.js //把此行 if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous() ...