Multiple profile files of Spingboot

Source: Internet
Author: User

1, when we write the main configuration file, the file name can be application-{profile}.properties/yml
default use of application.properties configuration;
In our project development, testing, on-line process, the inevitable need to repeatedly modify the configuration file. For example, the development environment connects the database IP and the test,
The production environment is different, the development environment development of the app version and test, the production line of the app version information is also different, and so on ..... We found that, despite the springboot in the
Use Application.properties (or YML) as the global profile for the entire project, but the production and test environments tend to be somewhat different from the configuration of the development environment.
This is where profile comes in handy.
Other than thatThere is also a clear use of profile--the ability to extract configuration information from application.yml to other files。 When our projects are getting bigger, the project
Too much configuration information, the APPLICATION.YML configuration file will be larger, then we can define profile, some configuration information from the APPLICATION.YML to extract
A separate file, as established:
APPLICATION-DEVFTP.YML only store configuration information related to the development environment FTP
APPLICATION-DEVREDIS.YML only store the development environment Redis-related configuration information
APPLICATION-TESTFTP.YML only store configuration information about the test environment FTP
APPLICATION-TESTREDIS.YML only store Redis-related configuration information for the test environment
Then in APPLICATION.YML, switch the development environment, configure Spring.profiles.active=dev, Devftp,devredis to introduce Dev, devftp, Devredis
Configuration. Switch the test environment, configure Spring.profiles.active=test, Testftp,testredis to introduce test, TESTFTP, Testredis configuration.

We are using a configuration file that ends in Yml, and YML supports multiple document blocks:

Server:   8080Spring:  profiles:    active:prod---server:  8081Spring:  Profiles:dev ---server:  8082Spring:  profiles:prod  #指定属于哪个环境

You can also take advantage of a configuration file that ends in properties:

?     1. Specify spring.profiles.active= dev in the configuration file  ?     2, command line:?         -jar Spring-boot-02-config-0.0.1-snapshot.jar--spring.profiles.active=Dev;//a command that runs on a cmd command line after a package project?        Can you configure the incoming command line parameters directly at the time of    the test? 3, virtual machine parameters;?         -dspring.profiles.active=dev//configuration runs in Inde virtual machines, virtual machine parameters start with the D letter
2. configuration file Loading location

Springboot boot scans the application.properties or application.yml file at the following location as the default profile for spring boot

–file:./config/

–file:./

–classpath:/config/

–classpath:/

Priority is high and high priority configuration overrides low-priority configuration;

Springboot will load the master configuration file from all four locations, and the complementary configuration;

We can also change the default configuration file location via spring.config.location;

After the project is packaged, we can use command-line arguments to specify a new location for the configuration file when the project is launched, and the configuration files that are loaded by default are used together to form a complementary configuration;

Java-jar Spring-boot-02-config-02-0.0.1-snapshot.jar--spring.config.location=g:/application.properties

3. External configuration loading Order

Springboot can also load configurations from the following locations, priority from high to low, high-priority configuration overrides low-priority configurations, and all configurations will form complementary configurations

1. Command-line arguments

All configurations can be specified on the command line

Java-jar Spring-boot-02-config-02-0.0.1-snapshot.jar--server.port=8087--SERVER.CONTEXT-PATH=/ABC

Multiple configurations separated by spaces;--Config item = value

2. Jndi Properties from Java:comp/env

3.Java System Properties (System.getproperties ())

4. Operating system Environment variables

random.* property value for 5.RandomValuePropertySource configuration

Search within jar package outward jar package, priority load with profile, then load without profile

Application-{profile}.properties or Application.yml (with Spring.profile) profile outside the 6.jar package

Application-{profile}.properties or Application.yml (with Spring.profile) profile inside the 7.jar package

Application.properties or Application.yml (without spring.profile) configuration file outside of the 8.jar package

9.jar application.properties or application.yml (without spring.profile) profile inside the package

[Email protected] @propertysource on the annotation class

11. Default properties specified by Springapplication.setdefaultproperties

Multiple profile files of Spingboot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.