Spring Boot Tutorial-Spring boot profiles for multi-environment configuration switching

Source: Internet
Author: User
Tags spring boot tutorial

In back-end development, applications may have different configurations in different environments, such as database connections, log levels, etc., development, testing, and production each environment may not be configured in a consistent configuration.

With spring boot profile, configuration switching can be implemented in multiple scenarios to facilitate testing and deployment of production environments in development. Here's a general introduction to how the Yml configuration file and the properties configuration file use profile to configure different environments. Development Environment JDK 1.8 Maven 3.x Spring boot 1.5.8 Intellij idea , using Spring boot Profiles 1. Using the Yml file

First, let's start by creating a property file named Application.yml, as follows:

Server:
  port:8080

my:
  name:demo

Spring:
  profiles:
    active:dev

---
#development Environment
Spring:
  profiles:dev

server:
  port:8160

my:
  name:ricky

---
#test Environment
Spring:
  profiles:test

server:
  port:8180

my:
  name:test

---
#production Environment
Spring:
  profiles:prod

server:
  port:8190

my:
  name: Prod

The Application.yml file is divided into four parts, using the---as the delimiter, the first part of the General configuration section, representing the properties common to three environments, the following three paragraphs are: development, testing, production, with spring.profiles specified a value (developed as Dev, Test, production is prod), this value indicates which profile should be used for the configuration of this segment.

If we are local boot, in the general configuration can be set to invoke which environment profil, that is, the first paragraph of the spring.profiles.active=xxx, where XXX is the spring.profiles corresponding value in the following 3 paragraphs, This allows you to control which environment's configuration file is started locally, for example:

Spring:
    Profiles:
        Active:dev

Indicates that the configuration of the development environment is loaded by default, and if Dev is changed to test, the properties of the test environment are loaded, and so on.

Note: If Spring.profiles.active does not specify a value, only the value that does not specify the Spring.profiles file is used, that is, only the generic configuration is loaded. Startup Parameters

If it is deployed to the server, we normally hit the jar package, start with--spring.profiles.active=xxx to control which environment to load the configuration, the complete command is as follows:

Java-jar Xxx.jar--spring.profiles.active=test represents the configuration using a test environment

Java-jar Xxx.jar--spring.profiles.active=prod Represents a configuration that uses a production environment
Configuring a properties file with multiple yml configuration files

We can also use multiple yml to configure properties, place environment-independent properties into the Application.yml file, and create application-{profile}.yml files with the same naming conventions as the configuration file to store different environment-specific configurations, such as APPLICATION-TEST.YML stores configuration properties specific to the test environment, application-prod.yml the configuration properties specific to the production environment.

In this form to configure the properties of multiple environments file, in the Application.yml file spring.profiles.active=xxx to specify the loading of different environments configuration, if not specified, By default, only the Application.yml property file is used, and no other profiles configuration is loaded. 2. Using the properties file

If you use Application.properties to configure multiple environments, the principle is consistent with multiple yml profiles, creating application-{profile}.properties files to store different environment-specific configurations, Place the environment-independent properties into the Application.properties file and specify the configuration for loading the different environments in the Application.properties file by Spring.profiles.active=xxx. If not specified, the application.properties configuration is loaded by default, and configuration with profile is not loaded. Second, Maven profile

If we are using a build tool that is MAVEN, you can also use the MAVEN Profile feature to enable multi-environment configuration packaging.

The Pom.xml configuration is as follows:

<profiles> <!--development environment--<profile> <id>dev</id> &LT;PR
            Operties> <build.profile.id>dev</build.profile.id> </properties> 
        <activation> <activeByDefault>true</activeByDefault> </activation>
            </profile> <!--test environment--<profile> <id>test</id> <properties> <build.profile.id>test</build.profile.id> &LT;/PROPERTIES&G
        T </profile> <!--production Environment--<profile> <id>prod</id> <p
        Roperties> <build.profile.id>prod</build.profile.id> </properties> </profile> </profiles> <build> &LT;FINALNAME&GT;${PROJECT.ARTIFACTID}&LT;/FINALNAME&G
        T &lT;resources> <resource> <directory>src/main/resources</directory>
                <filtering>false</filtering> </resource> <resource> <directory>src/main/resources.${build.profile.id}</directory> <filtering>false</fi
                ltering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-maven- Plugin</artifactid> <configuration> <classifier>exec</classifier&
                Gt </configuration> </plugin> </plugins> </build>

Specify which profile to use by executing mvn clean package-p ${profile}. References

Spring Boot Reference Guide-profiles

Maven Profiles

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.