Spring boot reads project parameter configuration

Ask Youlong time 2021-10-14 04:49:48

1、 brief introduction

spring boot Some parameters can be configured when the project starts , We can do that by implementing ApplicationRunner or CommandLineRunner Interface , And rewrite run Method to get these configurations , If there are multiple classes defined in the project and these two classes are implemented , adopt @Order Annotations to specify their execution order .

spring boot The project can specify project operation parameters at startup ,eg: java -jar --user=wenyl


2.1、 The source code parsing

ApplicationRunner Of run The method parameter is ApplicationArguments, There are five internal methods

public interface ApplicationArguments {
String[] getSourceArgs();
Set<String> getOptionNames();
boolean containsOption(String name);
List<String> getOptionValues(String name);
List<String> getNonOptionArgs();

We add startup parameters as follows


getSourceArgs() Space as separator , Get all configurations , And returns... As a string


getOptionNames() Method to get the configuration item ( Configure items to -- start ) The name of , The value returned by this method is set aggregate , Expected value of a configuration item , It could be an array , It needs to be defined separately, but name It's all the same


getOptionValues() Method is used to get the value of the configuration item , It returns an array , This means that we can define different values for the same configuration item , As we added name attribute , Two values are defined


getNonOptionArgs() Method is used to obtain the configuration item parameters , In the test configuration , There is one hello The parameter is No -- It's the only prefix you get


2.2、 Case study

public class MessageApplicationRunner1 implements ApplicationRunner {
public void run(ApplicationArguments args) throws Exception {
System.out.println(" Start loading ");
List<String> nonOptionArgs = args.getNonOptionArgs();
Set<String> optionNames = args.getOptionNames();
for(String optionName:optionNames){
System.out.println(" Parameters -"+optionName+" value :"+ args.getOptionValues(optionName));
String[] sourceArgs = args.getSourceArgs();
System.out.println(" End loading ");


CommandLineRunner No, ApplicationRunner So many operations , Just separated by spaces , Interception configuration ,2.1 The same configuration in , The information obtained is as follows


public class MessageApplicationRunner implements CommandLineRunner {
public void run(String... args) throws Exception {

4、ApplicationRunner and CommandLineRunner The difference between

ApplicationRunner Of run The parameters of the method are ApplicationArguments, and CommandLineRunner Of run The parameters of the method are String type


title :spring boot Read the project parameter configuration
author : wenyl
Address : http://www.wenyoulong.com/articles/2021/09/27/1632708755896.html

Please bring the original link to reprint ,thank
Similar articles