Several ways for Spring boot to activate profiles

Several ways for Spring boot to activate profiles

  1. Specify directly in the configuration file

     spring.profiles.active=test
    

    This method is very inflexible and will not be used in the actual development department.

  2. Use placeholders to replace when packaging, take mavne as an example:

    First add to the configuration file:

     [email protected]@
    

    Add configuration for different environment packaging in pom.xml:

       <profiles>
         <profile>
           <id>dev</id><!-- 开发环境 -->
           <properties>
             <package.target>dev</package.target>
           </properties>
           <activation>
             <activeByDefault>true</activeByDefault>
           </activation>
           <dependencies>
             <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-devtools</artifactId>
               <optional>true</optional>
             </dependency>
             <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger-ui</artifactId>
               <version>2.6.1</version>
             </dependency>
           </dependencies>
         </profile>
         <profile>
           <id>prod</id><!-- 生产环境 -->
           <properties>
             <package.target>prod</package.target>
           </properties>
         </profile>
         <profile>
           <id>test</id><!-- 测试环境 -->
           <properties>
             <package.target>test</package.target>
           </properties>
           <dependencies>
             <dependency>
               <groupId>io.springfox</groupId>
               <artifactId>springfox-swagger-ui</artifactId>
               <version>2.6.1</version>
             </dependency>
           </dependencies>
         </profile>
       </profiles>
       <build>
         <plugins>
           <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
         </plugins>
         <resources>
           <resource>
             <directory>src/main/resources</directory>
             <filtering>true</filtering><!-- 使用package.target值,替换配置文件中 @package.target@ -->
           </resource>
         </resources>
       </build>
    

    Execute the packaging command:

     mvn package -Ptest  
    

    Disadvantage: Profile must be specified every time it is packaged

  3. JVM parameter method:

    java command line:

     java -jar app.jar --spring.profiles.active=dev
    

    Add JAVA_OPS to catalina.bat in tomcat (without "set" in .sh). Select different profiles by setting active:

     set JAVA_OPTS="-Dspring.profiles.active=test"
    

    Start tomcat in eclipse. Right-click the project and add it to run as –> run configuration –> Arguments –> VM arguments.

     -Dspring.profiles.active="dev"
    
  4. web.xml method

     <init-param>
       <param-name>spring.profiles.active</param-name>
       <param-value>production</param-value>
     </init-param>
    
  5. Annotation method (junit unit testing is very practical)

     @ActiveProfiles({"unittest","productprofile"})
    
  6. ENV method (recommended to use this method)

    System environment variable SPRING_PROFILES_ACTIVE (note: uppercase)

    advantage:

    1. The application does not need to pay attention to the environment, the environment should be defined by the host or container;
    2. Deploying multiple applications on one host does not need to add configuration in each Tomcat's setenv.sh, or in each startup script: -Dspring.profiles.active=prod. When debugging locally on Windows, I don't want to set environment variables in eclipse, just right-click to debug;
    3. You only need to type a package to run in all environments, which can avoid a series of problems caused by typing the wrong package or deploying the wrong package.
{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324030177&siteId=291194637