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, bootstrap.properties and command-line parameters. Therefore, this paper sorted out some configuration loading methods and designed experiments to verify their priorities.
catalogue
Test code (total)
Configuration class:
@Component @PropertySource("/demo/demo.properties") @ConfigurationProperties("prioritytest") public class ConfigProperties { private String p1; private String p2; private String p3; private String p4; //Omit getter setter }
Controller class responsible for output results:
@RestController public class BookController { @Autowired ConfigProperties configProperties; @GetMapping("/testprop") public String testProp(){ String strBuild = configProperties.getP1() + " " + configProperties.getP2() + " " + configProperties.getP3() + " " + configProperties.getP4(); 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.
ServletContext
Description: the priority of ServletContext is between command line parameters and application.properties. 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:
@Bean public ServletContextInitializer servletContextInitializer() { return new ServletContextInitializer() { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.setInitParameter("prioritytest.p2", "p2_servlet_context"); servletContext.setInitParameter("prioritytest.p1", "p1_servlet_context"); } }; }
application.properties
Description: flexible and common configuration methods. This article only briefly demonstrates its usage. In fact, the relevant usage skills of application.properties include: yaml file supporting composite structure, application.properties file outside jar package, application.properties file inside jar package, and application-{profile}.properties usage, etc.
Test code:
Write in application.properties
prioritytest.p1 = p1_resources_application_properties prioritytest.p2 = p2_resources_application_properties prioritytest.p3 = p3_resources_application_properties
@PropertySources
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:
code:
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.