编程知识 cdmana.com

Different configuration of springboot

Generally speaking , Project development is divided into development environment (dev) Test environment (test) Preproduction environment (pre) Production environment (prod). There are some differences in configuration between different environments , We switch according to different deployment environment files , Let's talk about how to configure different environments .

Configuration file preparation

Prepare the corresponding application-*.properties file .

  • application.properties: Common configuration
  • application-dev.properties: Development environment configuration
  • application-test.properties: Test environment configuration
  • application-prod.properties: Production environment configuration

image.png

pom.xml To configure

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.yml</include>
            <include>**/*.xml</include>
        </includes>
        <!--  Whether to replace @xx@ It means maven properties Property value  -->
        <filtering>true</filtering>
    </resource>
</resources>

application.properties

#  At this point, the configuration is the development environment 
spring.profiles.active=dev

#  The test environment is configured at this time 
spring.profiles.active=test

#  At this point, the configuration is the production environment 
spring.profiles.active=prod

The above configuration requires us to modify the corresponding values in different environments , This is very inconvenient , At this point, we can configure it by variables , Now let's make some changes .

pom.xml Add

<profiles>
    <!-- development environment -->
 <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
    </profile>
    <!-- Test environment -->
 <profile>
        <id>test</id>
        <properties>
            <env>test</env>
        </properties>
    </profile>
    <!-- Production environment -->
 <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>

application.properties It is amended as follows

#  At this point through env To distinguish between different environments 
spring.profiles.active=@env@

At this time we are IDEA On the right you see the following :

Here we can choose the corresponding environment

image.png

Now we can switch Profiles To switch the environment . But in this case, it can only be started by running the startup class , like this :

image.png

Only in this way can it be applied to Profiles Selected value .

spring-boot:run Mode start

It can't be applied to IDEA On the right Profiles Environmental value of .


  • mvn spring-boot:run -P dev

We need to add the specified environment after the command , like this :mvn spring-boot:run -P dev Specified in dev Start up in the environment of , among -P Appoint pom.xml Corresponding profiles Value . here application.properties The configuration file is no longer valid .


springboot1.x start-up

  • mvn spring-boot:run -Drun.profiles=dev

springboot2.x start-up

  • mvn spring-boot:run -Dspring-boot.run.profiles=test

function jar package

java -jar -Dspring.profiles.active=test demo.jar

#  perhaps 

java -jar --spring.profiles.active=test demo.jar

版权声明
本文为[Poetry in the wind]所创,转载请带上原文链接,感谢

Scroll to Top