编程知识 cdmana.com

Spring boot reads project parameter configuration

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、ApplicationRunner

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

image.png

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

image.png

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

image.png

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

image.png

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

image.png

2.2、 Case study

@Component
public class MessageApplicationRunner1 implements ApplicationRunner {


    @Override
    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 ");
    }
}

3、CommandLineRunner

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

image.png

@Component
public class MessageApplicationRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println(args);
    }
}

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

image.pngimage.png


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

版权声明
本文为[Ask Youlong time]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002150219410G.html

Scroll to Top