欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot實(shí)現(xiàn)profiles動態(tài)切換的示例

 更新時間:2020年10月12日 10:56:17   作者:hythzx  
Spring Boot支持在不同的環(huán)境下使用不同的配置文件,該技術(shù)非常有利于持續(xù)集成,在構(gòu)建項(xiàng)目的時候只需要使用不同的構(gòu)建命令就可以生成不同運(yùn)行環(huán)境下war包,而不需要手動切換配置文件。

具體做法:

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指開發(fā)模式,prod指生產(chǎn)模式,如需其他模式,只需要添加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工具類來添加默認(rèn)啟動配置文件:

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的構(gòu)建的時候會替換#spring.profiles.active#

5、修改項(xiàng)目的啟動類:

@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 默認(rèn)dev模式切換成功。

6、構(gòu)建項(xiàng)目:

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

以上就是spring boot實(shí)現(xiàn)profiles動態(tài)切換的示例的詳細(xì)內(nèi)容,更多關(guān)于spring boot實(shí)現(xiàn)profiles動態(tài)切換的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中如何用Stream分組并求各組數(shù)量

    Java中如何用Stream分組并求各組數(shù)量

    這篇文章主要給大家介紹了關(guān)于Java中如何用Stream分組并求各組數(shù)量的相關(guān)資料,文中通過實(shí)例代碼,對大家學(xué)習(xí)或者Java具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • Spring web集成rabbitmq代碼實(shí)例

    Spring web集成rabbitmq代碼實(shí)例

    這篇文章主要介紹了Spring web集成rabbitmq代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • MyBatis獲取自動生成的(主)鍵值的方法

    MyBatis獲取自動生成的(主)鍵值的方法

    本文主要介紹了MyBatis獲取自動生成的(主)鍵值的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 手寫java性能測試框架的實(shí)現(xiàn)示例

    手寫java性能測試框架的實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了java實(shí)現(xiàn)性能測試框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 源碼解析springbatch的job運(yùn)行機(jī)制

    源碼解析springbatch的job運(yùn)行機(jī)制

    這篇文章主要介紹了springbatch的job是如何運(yùn)行的,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • springcloud項(xiàng)目改名的操作方法

    springcloud項(xiàng)目改名的操作方法

    這篇文章主要介紹了springcloud項(xiàng)目改名的操作方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Java中\(zhòng)n和\r區(qū)別

    Java中\(zhòng)n和\r區(qū)別

    本文主要介紹了Java中\(zhòng)n和\r區(qū)別。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • Java接收前端請求體的多種方式總結(jié)

    Java接收前端請求體的多種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java接收前端請求體的多種方式,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者Java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-08-08
  • 一小時迅速入門Mybatis之增刪查改篇

    一小時迅速入門Mybatis之增刪查改篇

    這篇文章主要介紹了迅速入門Mybatis之增刪查改篇,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Spring?中的InitializingBean使用示例

    Spring?中的InitializingBean使用示例

    InitializingBean?是?Spring?框架中的一個接口,用于在?Spring?容器中初始化?bean?時執(zhí)行特定的初始化邏輯,這篇文章主要介紹了Spring?中的InitializingBean使用示例,需要的朋友可以參考下
    2024-08-08

最新評論