java执行commandline_SpringBoot启动时执行特定方法(CommandLineRunner、ApplicationRunner)...

有时一些特殊的任务需要在系统启动时执行,例如配置文件加载、数据库脚本,启动定时任务等操作。SpringBoot提供了两种解决方案:CommandLineRunner 和 ApplicationRunner。二者使用方式大体一致,差别主要体现在参数上。

CommandLineRunner

SpringBoot项目在启动时会遍历所有的CommandLineRunner的实现类并调用其中的run方法。

如果整个系统中有多个CommandLineRunner的实现类,那么可以使用@Order注解对这些实现类的调用顺序进行排序,数字越小越先执行。

run方法的参数是系统启动是传入的参数,即入口类中main方法的参数。在调用SpringApplication.run方法时被传入 SpringBoot项目中。

ApplicationRunner

ApplicationRunner用法和CommandLineRunner差不多。项目在启动时会遍历所有的ApplicationRunner的实现类并调用其中的run方法。同样可以使用@Order注解对这些实现类的调用顺序进行排序,数字越小越先执行。

ApplicationRunner与CommandLineRunner的区别主要体现在run方法的参数上。不同于CommandLineRunner中的 run方法的数组参数,ApplicationRunner里run方法的参数是一个ApplicationArguments对象。

ApplicationArguments区分选项参数和非选项参数:

对于非选项参数:我们可以通过ApplicationArguments的getNonOptionArgs()方法获取,获取到的是一个数组。

对于选项参数:可以通过ApplicationArguments的getOptionNames()方法获取所有选项名称。通过 getOptionValues()方法获取实际值(它会返回一个列表字符串)。

1、编写CommandLineRunner代码,输出启动时传入的参数打印出来。

@Component

@Order(1)

public class CommandLineRunner1 implements CommandLineRunner {

@Override

public void run(String... args) throws Exception {

System.out.println("CommandLineRunner1:" + Arrays.toString(args));

}

}

@Component

@Order(2)

public class CommandLineRunner2 implements CommandLineRunner {

@Override

public void run(String... args) throws Exception {

System.out.println("CommandLineRunner2:" + Arrays.toString(args));

}

}

2、编写ApplicationRunner代码,把启动时传入的参数打印出来。

@Component

@Order(1)

public class ApplicationRunner1 implements ApplicationRunner {

@Override

public void run(ApplicationArguments args) throws Exception {

List nonOptionArgs = args.getNonOptionArgs();

System.out.println("Runner1[非选项参数]: " + nonOptionArgs);

Set optionNames = args.getOptionNames();

for(String optionName: optionNames) {

System.out.println("ApplicationRunner1[选项参数] name:" + optionName + ";value:" + args.getOptionValues(optionName));

}

}

}

@Component

@Order(2)

public class ApplicationRunner2 implements ApplicationRunner {

@Override

public void run(ApplicationArguments args) throws Exception {

List nonOptionArgs = args.getNonOptionArgs();

System.out.println("ApplicationRunner2[非选项参数]: " + nonOptionArgs);

Set optionNames = args.getOptionNames();

for(String optionName: optionNames) {

System.out.println("ApplicationRunner2[选项参数] name: " + optionName + "; value: " + args.getOptionValues(optionName));

}

}

}

3、配置系统启动时需要传入的参数,在页中编辑Program arguments栏目,在里面填写需要传入的参数。如果有多个参数,参数之间使用空格隔开。

2d429d484686520a8002001bcc0e725e.png

1a44b2219464761e023e505ca2745bca.png

如果我们将项目打包,以 jar 包的形式运行。那么这些参数可以跟在启动命令后面:

java -jar test.jar Hello Runner1 Runner2 --name=piao

4、验证结果。

b1f9e6f03182bff4cd6c15c54b9cec17.png

看到我们这个结果就成功了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值