原文网址:SpringBoot--启动时执行--方案大全/方法//实例_IT利刃出鞘的博客-CSDN博客
简介
在SpringBoot中我们有时候需要让项目在启动时提前加载相应的数据或者执行某个方法。本文介绍如何在启动时执行,方案很全,且都有实例。
法1:实现ApplicationRunner接口(推荐)
继承Application接口后项目启动时会按照执行顺序执行run方法。可以定义多个applicationrunner bean。在同一应用程序上下文中,可以使用有序接口或@order注释对其进行排序。
自动执行的方法
@Component
@Order(1)
public class MyTest implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("this is ApplicationRunner:run");
}
}
我们也可以设置Order来设定执行的顺序,在上面两个代码中也有,分别是注解@Order(value = 1)和实现接口implements Ordered的方式,个人喜欢注解,简单明了。实现接口implements Ordered的方式如下:
@Component
public class MyTest implements ApplicationRunner, Ordered {
@Override
public int getOrder() {
return 1;
}
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("this is run");
}
}
传递参数示例
–getOptionNames()方法可以得到foo这样的key的集合。
–getOptionValues(String name)方法可以得到bar这样的集合的value
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class MyApplicationRunner implements ApplicationRunner{
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs()));
System.out.println("===getOptionNames========"+args.getOptionNames());
System.out.println("===getOptionValues======="+args.getOptionValues("foo"));
System.out.println("==getOptionValues========"+args.getOptionValues("developer.name"));
}
}
配置参数启动:
--foo=bar --developer.name=xiao.qiang
运行结果:
===MyApplicationRunner===[--foo=bar, --developer.name=xiao.qiang]
===getOptionNames========[foo, developer.name]
===getOptionValues=======[bar]
==getOptionValues========[xiao.qiang]
法2:实现CommandLineRunner接口(推荐)
CommandLineRunner和ApplicationRunner的作用是相同的。不同点在于,前者的run方法参数是String...args,直接传入字符串后者的参数是ApplicationArguments,对参数进行了封装。这些参数是启动spring boot程序时传给main()方法的。
ApplicationArguments是对参数(main方法)做了进一步的处理,可以解析–name=value的,我们就可以通过name来获取value,而CommandLineRunner只是获取–name=value而不解析。
自动运行的方法
@Component
@Order(value = 1)
public class MyTest implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("this is CommandLineRunner");
}
}
传递参数示例
import java.util.Arrays;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CommandLineRunnerBean implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineRunnerBean.class);
@Override
public void run(String... args) {
String strArgs = Arrays.stream(args).collect(Collectors.joining("|"));
logger.info("Application started with arguments:" + strArgs);
}
}
使用带有参数的可执行jar运行程序。spring-boot-demo-0.0.1-SNAPSHOT.jar为生成的jar文件。执行命令如下:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3
输出结果
2017-03-19 13:38:38.909 INFO 1036 --- [ main] c.c.bean.CommandLineRunnerBean : Application started with arguments:data1|data2|data3
法3:实现ApplicationListener接口(推荐)
上边是文章的部分内容,为便于维护,全文已转移到此网址:SpringBoot-启动时执行的方法 - 自学精灵