SpringBoot series of six: the configuration file is loaded

First, the configuration file is loaded position
 scans application.properties or application.yml files in the following locations when SpringBoot started as SpringBoot default configuration file.
file:./config/
file:./
classpath:/config/
classpath:/
In the end of a high priority, a high priority configuration overwrites the low priority configuration; SpringBoot main configuration file is loaded from the four positions; complementary configuration;

Test:
 respectively file:./config/, file:./, classpath:/config/, classpath:/created under application.properties profiles are set server.portas follows: 8084,8083,8082,8081. Start testing project, the project started in 8084 port; commented file:./config/under application.properties in server.portthe configuration, and then start the project started in 8083 port; commented file:./under application.properties in server.portthe configuration, and then start the project started in 8082 port; commented classpath:/config/under application.properties in server.portthe configuration, and then start the project started in 8083 port.
Here Insert Picture Description
We in classpath:/the new configuration application.properties configuration file under:

server.port=8081

#配置项目的访问路径
server.servlet.context-path=/boot02

Creating HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
       return "hello";
    }
}

 Restart the project, access the browser http: // localhost: 8084 / hello found not visit. Visit http: // localhost: 8084 / boot02 / hello can access. DESCRIPTION complementary configuration formed between several profiles.
 We can also spring.config.locationchange the default configuration file location. Items packed in the future, we can use the command-line parameters in the form of, when the project started to specify the new location of the configuration file; specified in the configuration file and the default load of these configuration files work together to form a complementary configuration;

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/springboot/application.properties

Second, the order of loading configuration files
 SpringBoot may also be loaded from the arranged position; low priority level; high priority is configured to cover a low priority configuration, and any configuration may complement the configuration. The official document: https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#boot-features-external-config
1, the command-line parameters
 All configuration can be in the command line on specified. A plurality of configuration separated by spaces; --配置项=值.

 java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc 

2, from the java: comp / env in JNDI properties.
3, Java system properties (System.getProperties ()).
4, the operating system environment variables.
5, random RandomValuePropertySource configuration. * Attribute value.
6, looking for outgoing packets within a jar jar package; preferentially loaded with the profile:
 jar package outside application- {profile} .properties or application.yml (with spring.profile) profile.
 internal jar package application- {profile} .properties or application.yml (with spring.profile) profile.
7, without reloading the profile:
 JAR application.properties outside the package or application.yml (without spring.profile) profile.
 internal jar package application.properties or application.yml profile (without spring.profile).
10, @PropertySource on the @ Configuration annotation class.
11, by SpringApplication.setDefaultProperties specified default properties.

Test:
New Profile application.properties outside the jar package

server.port=8088

#配置项目的访问路径
server.servlet.context-path=/boot

Here Insert Picture Description
Using java -jar to start the project.java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/lizhiqiang1217/article/details/91347464