最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年10月12日 10:56:17   作者:hythzx  
Spring Boot支持在不同的環(huán)境下使用不同的配置文件,該技術(shù)非常有利于持續(xù)集成,在構(gòu)建項(xiàng)目的時(shí)候只需要使用不同的構(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>

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

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)建的時(shí)候會替換#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"));
  }
}

以上修改完成之后,在啟動的時(shí)候會顯示: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的SPI機(jī)制(推薦)

    一文搞懂Java的SPI機(jī)制(推薦)

    Java定義了一套JDBC的接口,但并未提供具體實(shí)現(xiàn)類,而是在不同云廠商提供的數(shù)據(jù)庫實(shí)現(xiàn)包。這篇文章給大家介紹Java的SPI機(jī)制,感興趣的朋友一起看看吧
    2021-11-11
  • Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例

    Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例

    這篇文章主要介紹了Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • Java泛型 <T> T、 T、<T>的用法小結(jié)

    Java泛型 <T> T、 T、<T>的用法小結(jié)

    T在Java泛型中,被稱作類型變量, 有的方法返回值是<T> T,有的是T,區(qū)別在哪里,本文主要介紹了Java泛型 <T> T、 T、<T>的用法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解下
    2023-12-12
  • SpringBoot 嵌入式web容器的啟動原理詳解

    SpringBoot 嵌入式web容器的啟動原理詳解

    這篇文章主要介紹了SpringBoot 嵌入式web容器的啟動原理詳解,具有很好的參考價(jià)值,希望對大家有所幫助。
    2021-11-11
  • Java實(shí)現(xiàn)簡易畫圖板

    Java實(shí)現(xiàn)簡易畫圖板

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡易畫圖板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • IDEA在plugins里搜不到mybatisx插件的解決方法

    IDEA在plugins里搜不到mybatisx插件的解決方法

    本文主要介紹了IDEA在plugins里搜不到mybatisx插件的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • spring-boot-maven-plugin?配置有啥用

    spring-boot-maven-plugin?配置有啥用

    這篇文章主要介紹了spring-boot-maven-plugin?配置是干啥的,這個(gè)是SpringBoot的Maven插件,主要用來打包的,通常打包成jar或者war文件,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Java經(jīng)典面試題匯總:網(wǎng)絡(luò)編程

    Java經(jīng)典面試題匯總:網(wǎng)絡(luò)編程

    本篇總結(jié)的是Java 網(wǎng)絡(luò)編程相關(guān)的面試題,后續(xù)會持續(xù)更新,希望我的分享可以幫助到正在備戰(zhàn)面試的實(shí)習(xí)生或者已經(jīng)工作的同行,如果發(fā)現(xiàn)錯(cuò)誤還望大家多多包涵,不吝賜教,謝謝
    2021-07-07
  • 通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

    通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

    通過分析java.util.TreeMap源碼來對經(jīng)典問題紅黑樹加強(qiáng)理解和理清思路。
    2017-11-11
  • Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解

    Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解

    這篇文章主要介紹了Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解,Spring的設(shè)計(jì)非常優(yōu)雅,有很多的擴(kuò)展點(diǎn)供我們對項(xiàng)目進(jìn)行擴(kuò)展,今天學(xué)習(xí)一下Spring其中擴(kuò)展點(diǎn)之一的BeanFactoryPostProcessor,需要的朋友可以參考下
    2023-11-11

最新評論

景东| 广州市| 开阳县| 陇南市| 达州市| 大宁县| 朝阳市| 扶沟县| 白水县| 沿河| 阿拉善盟| 平原县| 梁平县| 江北区| 芦山县| 瑞昌市| 江西省| 青浦区| 郎溪县| 邹城市| 横峰县| 青冈县| 台安县| 定州市| 阿克苏市| 河曲县| 巨鹿县| 潮州市| 靖安县| 三河市| 巴青县| 黄石市| 建湖县| 景泰县| 新乡市| 河间市| 乐陵市| 门源| 大冶市| 云龙县| 扎赉特旗|