7. Configuration file loading location
The previous section described Spring Boot loading configuration files from within the project, and this section focuses on loading configuration files from outside.
- command line arguments
All configurations can be specified on the command line
java -jar spring-boot-02-config-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc
Multiple configurations are separated by spaces; --configuration item=value
Of course, since it is not necessarily safe to use command-line parameters or modify the default property configuration, you can prohibit the use of the command-line through code.
SpringApplication application = new SpringApplication(SpringBoot02ConfigApplication.class); //Prohibit modifying default configuration properties through command line parameters application.setAddCommandLineProperties(false); //start up application.run(args);
Java System Properties (System.getProperties())
operating system environment variables
random.* property values configured by RandomValuePropertySource
Priority loading with profile
The application-{profile}.properties or application.yml (with spring.profile) configuration file outside the jar package
The application-{profile}.properties or application.yml (with spring.profiles) configuration file inside the jar package
Then load without profile
The application.properties or application.yml (without spring.profile) configuration file outside the jar package
The application.properties or application.yml (without spring.profile) configuration file inside the jar package
@PropertySource on @Configuration annotated classes
Default properties specified via SpringApplication.setDefaultProperties
// use the specified configuration file SpringApplication application = new SpringApplication(SpringBoot02ConfigApplication.class); //Load the specified configuration file InputStream is = SpringBoot02ConfigApplication.class.getClassLoader().getResourceAsStream("app.properties"); Properties properties = new Properties(); try { properties.load(is); } catch (IOException e) { e.printStackTrace (); } // set properties application.setDefaultProperties(properties); //start up application.run(args);
=====================Make an advertisement, welcome to pay attention =====================
QQ: |
412425870 |
WeChat public account: Cay Classroom |
|
csdn blog: |
http://blog.csdn.net/caychen |
Code cloud: |
https://gitee.com/caychen/ |
github: |
https://github.com/caychen |
Click on the group number or scan the QR code to join the QQ group: |
|
Click on the group number or scan the QR code to join the QQ group: |