Priority experiment of spring boot configuration loading mode (part)

Keywords: Java java web

In the interview two days ago, the interviewer asked Springboot what kinds of loading and configuration methods are available and how the priorities of these methods are arranged. I was bored when I only thought of Apollo, and command-line parameters. Therefore, this paper sorted out some configuration loading methods and designed experiments to verify their priorities.

Official documents


Test code (total)

Command line arguments



experimental result

Test code (total)

Configuration class:

public class ConfigProperties {
    private String p1;
    private String p2;
    private String p3;
    private String p4;

    //Omit getter setter

Controller class responsible for output results:

public class BookController {

    ConfigProperties configProperties;

    public String testProp(){
        String strBuild = configProperties.getP1() +
                " " +
                configProperties.getP2() +
                " " +
                configProperties.getP3() +
                " " +
        return strBuild;


Command line arguments

Description: command line parameters

By default, SpringApplication converts any command line option arguments (that is, arguments starting with --, such as --server.port=9000) to a property and adds them to the Spring Environment.

  Spring application will convert the command-line parameters to property and store them in the spring Environment.

If you do not want command line properties to be added to the Environment, you can disable them by using SpringApplication.setAddCommandLineProperties(false).

  You can prevent Spring from adding command-line parameters to the Environment in the above way.

For Spring Environment, please refer to Official documents.

Or reference This article.

Simply understand it as an integration of JDK environment, Servlet environment and Spring environment.

Test code: since the author's test project uses Maven to start the Springboot application, first refer to Related documents Confirm the parameter writing method, and then add two goals as shown in the figure in the IDEA - project window - Run Maven- new goal, which can be used normally.


Description: the priority of ServletContext is between command line parameters and Many blogs mentioned the method of adding attributes to ServletContext through xml files. Of course, there are also methods that do not depend on xml files.

I haven't found any special application scenarios for ServletContext yet

Test code:

reference resources

    public ServletContextInitializer servletContextInitializer() {

        return new ServletContextInitializer() {

            public void onStartup(ServletContext servletContext) throws ServletException {


Description: flexible and common configuration methods. This article only briefly demonstrates its usage. In fact, the relevant usage skills of include: yaml file supporting composite structure, file outside jar package, file inside jar package, and application-{profile}.properties usage, etc.

Test code:

Write in

prioritytest.p1 = p1_resources_application_properties
prioritytest.p2 = p2_resources_application_properties
prioritytest.p3 = p3_resources_application_properties


Description: this annotation allows a Configuration class (@ Configuration)   classes) read Configuration information from the specified file, and its priority is lower than the above-mentioned Configuration methods.

In addition, the official document explains the time when the annotation fills the value into the attribute:

Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.

Test code:

Project structure diagram:


prioritytest.p1 = p1_resources_propertysource
prioritytest.p2 = p2_resources_propertysource
prioritytest.p3 = p3_resources_propertysource
prioritytest.p4 = p4_resources_propertysource

experimental result

Comply with the description of configuration priority in the document.

Posted by justice1 on Wed, 13 Oct 2021 18:41:47 -0700