Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yj1499945/article/details/87184890
spring boot using 1.5.9
private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
Iterator var4 = (new LinkedHashSet(runners)).iterator();
while(var4.hasNext()) {
Object runner = var4.next();
if (runner instanceof ApplicationRunner) {
this.callRunner((ApplicationRunner)runner, args);
}
if (runner instanceof CommandLineRunner) {
this.callRunner((CommandLineRunner)runner, args);
}
}
}
Can be seen here will find ApplicationRunner and CommandLineRunner implementation class, they can go run some operations in this process
Take a look at CommandLineRunner class
@Component
public class OtherOperation implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
System.out.println("qwe");
for (String s : strings){
System.out.println(s);
}
}
}
Roughly like this, he can get the parameters passed in and then you do some custom operations.
If I start the command java -jar xxx.jar username = xxx, then passed in parameter is username = xxx, you can do some operation on this parameter and so on.
Of course, with the command line parameters passed priority than .yml and .properties (avoid pit)