溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用spring boot實現profiles動態切換

發布時間:2020-10-28 20:57:04 來源:億速云 閱讀:396 作者:Leah 欄目:開發技術

這篇文章將為大家詳細講解有關使用spring boot實現profiles動態切換,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

具體做法:

1、首先在pom中添加profiles:

<profiles>
  <profile>
   <id>dev</id>
   <activation>
     <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
     <spring.profiles.active>dev</spring.profiles.active>
   </properties>
   <dependencies>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
     </dependency>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
     </dependency>
   </dependencies>
  </profile>
 
  <profile>
   <id>prod</id>
   <dependencies>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
     </dependency>
   </dependencies>
   <properties>
     <spring.profiles.active>prod</spring.profiles.active>
   </properties>
  </profile>
</profiles>

dev指開發模式,prod指生產模式,如需其他模式,只需要添加profile即可.

2、在pom.xml的build中添加plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>${maven-resources-plugin.version}</version>
  <executions>
   <execution>
     <id>default-resources</id>
     <phase>validate</phase>
     <goals>
      <goal>copy-resources</goal>
     </goals>
     <configuration>
      <outputDirectory>target/classes</outputDirectory>
      <useDefaultDelimiters>false</useDefaultDelimiters>
      <delimiters>
        <delimiter>#</delimiter>
      </delimiters>
      <resources>
        <resource>
         <directory>src/main/resources/</directory>
         <filtering>true</filtering>
         <includes>
           <include>**/*.xml</include>
           <include>**/*.yml</include>
         </includes>
        </resource>
        <resource>
         <directory>src/main/resources/</directory>
         <filtering>false</filtering>
         <excludes>
           <exclude>**/*.xml</exclude>
           <exclude>**/*.yml</exclude>
         </excludes>
        </resource>
      </resources>
     </configuration>
   </execution>
  </executions>
</plugin>

該配置用來在打包的時候修改配置文件。

3、編寫DefaultProfileUtil工具類來添加默認啟動配置文件:

import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * Utility class to load a Spring profile to be used as default
 * when there is no <code>spring.profiles.active</code> set in the environment or as command line argument.
 * If the value is not available in <code>application.yml</code> then <code>dev</code> profile will be used as default.
 */
public final class DefaultProfileUtil {
 
  private static final String SPRING_PROFILE_DEFAULT = "spring.profiles.default";
 
  private DefaultProfileUtil(){
  }
 
  /**
   * Set a default to use when no profile is configured.
   *
   * @param app the spring application
   */
  public static void addDefaultProfile(SpringApplication app) {
    Map<String, Object> defProperties = new HashMap<>();
    /*
    * The default profile to use when no other profiles are defined
    * This cannot be set in the <code>application.yml</code> file.
    * See https://github.com/spring-projects/spring-boot/issues/1219
    */
    defProperties.put(SPRING_PROFILE_DEFAULT, Constants.SPRING_PROFILE_DEVELOPMENT);
    app.setDefaultProperties(defProperties);
    System.out.println(app);
  }
 
  /**
   * Get the profiles that are applied else get default profiles.
   */
  public static String[] getActiveProfiles(Environment env) {
    String[] profiles = env.getActiveProfiles();
    if (profiles.length == 0) {
      return env.getDefaultProfiles();
    }
    return profiles;
  }
}
public class Constants {
 
  public static final String SPRING_PROFILE_DEVELOPMENT = "dev";
  public static final String SPRING_PROFILE_PRODUCTION = "prod";
  private Constants() {
  }
}

4、修改application.yml配置文件,添加(采用application.properties文件):

spring:
  profiles:
   active: #spring.profiles.active#

maven的構建的時候會替換#spring.profiles.active#

5、修改項目的啟動類:

@SpringBootApplication
public class Demo1Application {
 
  private static final Logger log = LoggerFactory.getLogger(Demo1Application.class);
 
  public static void main(String[] args) {
   SpringApplication app = new SpringApplication(Demo1Application.class);
   DefaultProfileUtil.addDefaultProfile(app);
   Environment env = app.run(args).getEnvironment();
   log.info("\n----------------------------------------------------------\n\t" +
         "Application '{}' is running! Access URLs:\n\t" +
         "Local: \t\thttp://localhost:{}\n\t" +
         "----------------------------------------------------------",
      env.getProperty("spring.application.name"),
      env.getProperty("server.port"));
  }
}

以上修改完成之后,在啟動的時候會顯示:The following profiles are active: dev 默認dev模式切換成功。

使用spring boot實現profiles動態切換

6、構建項目:

采用mvn clean package -Pprod命令構建,最后的配置文件會被改成:

使用spring boot實現profiles動態切換

關于使用spring boot實現profiles動態切換就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女