摘要:本文介绍了如何通过CommanLineRunner和ApplicationRunner来实现系统启动任务。
CommanLineRunner
我们来创建自定义的MyCommanLineRunner类,并实现CommanLineRunner接口即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.example.commandlinerunner;
import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component @Order(99) public class MyCommandLineRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(this.toString() + Arrays.toString(args)); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.example.commandlinerunner;
import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component @Order(100) public class MyCommandLineRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(this.toString() + Arrays.toString(args)); } }
|
我们在Terminal中运行命令
将项目打包成jar包,然后运行
1
| java -jar commandlinerunner-0.0.1-SNAPSHOT.jar ThreeBody BallLightning ChineseSun
|
通过命令行的方式传递args参数,在控制台我们可以看到以下信息,即系统启动的时候会自动运行两个CommanLineRunner类的run方法。
ApplicatinoRunner
我们再通过ApplicationRunner来实现系统启动,与上面一样创建自定义类,并实现ApplicationRunner接口即可。我们同样也可以通过@Order来设置其优先级。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.example.commandlinerunner;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.List; import java.util.Set;
@Component @Order(99) public class MyApplicationRunner1 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { String[] sourceArgs = args.getSourceArgs(); System.out.println("sourceArgs: " + Arrays.toString(sourceArgs)); List<String> noOptionArgs = args.getNonOptionArgs(); System.out.println("nonOptionArgs: " + noOptionArgs); Set<String> optionNames = args.getOptionNames(); System.out.println("--------------------------"); for (String name : optionNames) { System.out.println(name + ": " + args.getOptionValues(name)); } System.out.println("End: " + this.toString()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.example.commandlinerunner;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.List; import java.util.Set;
@Component @Order(100) public class MyApplicationRunner2 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { String[] sourceArgs = args.getSourceArgs(); System.out.println("sourceArgs: " + Arrays.toString(sourceArgs)); List<String> noOptionArgs = args.getNonOptionArgs(); System.out.println("nonOptionArgs: " + noOptionArgs); Set<String> optionNames = args.getOptionNames(); System.out.println("--------------------------"); for (String name : optionNames) { System.out.println(name + ": " + args.getOptionValues(name)); } System.out.println("End: " + this.toString()); } }
|
同时CommanLineRunner与ApplicationRunner不冲突,两者可以共存,ApplicationRunner优先级高于CommanLineRunner。我们来运行验证一下,同样先打包成jar包,然后在Terminal运行
1
| java -jar commandlinerunner-0.0.1-SNAPSHOT.jar ThreeBody BallLightning ChineseSun
|
我们再来看看若设置参数名称会怎么样,在Terminal运行
1
| java -jar commandlinerunner-0.0.1-SNAPSHOT.jar --name=ThreeBody --name=BallLightning --name=ChineseSun --author=LiuCiXin Spring SpringBoot SpringMVC
|
我们可以看到,ApplicationRunner可以识别OptionArgs,但CommanLineRunner不能识别,而将其全部视为noOptionArgs