springboot-15-启动时加载数据的方法CommandLineRunner

时间:2023-03-10 03:15:47
springboot-15-启动时加载数据的方法CommandLineRunner

有时需要爱项目启动时, 去加载一些配置文件什么的, 可以使用监听器的方式加载, 这是可以通过实现接口 CommandLineRunner来实现需求:

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

package com.iwhere.run;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* 该类可以在springboot启动是, 进行操作
* @Order: 启动时执行的顺序, 小的优先执行
* @author wenbronk
* @time 2017年4月6日 下午1:40:17 2017
*/ @Component
@Order(value=)
public class MyStartupRunner implements CommandLineRunner { /**
* 执行的方法,
* args为执行时传入的参数,
* 可用: SpringApplication.run(App.class, new String[]{"hello,","林峰"});
* 或者: eclipse中给java应用传args参数的方法如下:
1、先写好Java代码,比如文件名为IntArrqy.java;
2、在工具栏或菜单上点run as下边有个Run Configuration;
3、在弹出窗口点选第二个标签arguments;
4、把你想输入的参数写在program argumenst就可以了,多个参数使用空格隔开。
完成后点run即可通过运行结果看到参数使用情况了。
*/
@Override
public void run(String... arg0) throws Exception {
System.out.println("开机服务执行的操作....");
}
}

原文地址: http://412887952-qq-com.iteye.com/blog/2292577