springboot不同環境不同配置

方式一:最簡單的方式是直接使用外部的配置文件

在啓動參數中添加spring.config.location變量, 指定配置文件路徑

java -jar -Dspring.config.location=C:\Users\sk-qianxiao\Desktop\application-dev.properties demo-0.0.1-SNAPSHOT.jar

 

方式二:設置環境變量使用程序類不同環境配置文件

不同環境的配置文件命名規則如下

application-dev.properties:默認配置文件

application-dev.properties:開發環境

application-test.properties:測試環境

application-prod.properties:生產環境

 

指定變量設置環境

java -jar -Dspring.profiles.active=dev demo-0.0.1-SNAPSHOT.jar

注意:沒有設置 或 設置 錯誤使用的是默認配置文件

 

方式三:使用maven在打包時進行配置填充

maven方式原文鏈接:https://www.cnblogs.com/yucy/p/7082042.html

maven提供了一組屬性以供開發人員靈活搭配,可以根據環境來打包,比如測試環境:mvn package -DskipTests -P test,-P也就是指定profile裏面id爲test的子項配置來打包。在pom文件裏面,可以指定多套配置文件,下面例子中區分了三套配置文件,不指定-P則默認爲dev。其中的env相當於屬性文件中的env=test,也可以多指定多個屬性,看實際需要。至於爲什麼不在相應的屬性文件中添加類似env這樣的配置,下文會提到。

<!-- 區分環境打包 -->
   <profiles>
       <!-- 開發環境[默認] -->
       <profile>
           <id>dev</id>
           <properties>
               <env>dev</env>
           </properties>
           <activation>
               <activeByDefault>true</activeByDefault>
           </activation>
       </profile>
       <!-- 測試環境 -->
       <profile>
           <id>test</id>
           <properties>
               <env>test</env>
           </properties>
       </profile>
       <!-- 線上環境 -->
       <profile>
           <id>product</id>
           <properties>
               <env>product</env>
           </properties>
       </profile>
   </profiles>

resources和filters,maven中的這兩個屬性通常是搭配起來使用的,filter這裏可以理解爲篩選器或者填充器。filters裏面可以配置多個filter,但是會以最後一個爲準,所以一般只配置一個filter,看下面例子。例子有${env},打包的時候,根據-P test指定,-P 後面的參數與<profile>的<id>一一對應,再根據此ID,到<profile> 的 <properties>得到env屬性值,即<env>test</env>。上文指定了三套配置,所以項目裏面也需要有這三個配置文件:dev.properties , test.properties , product.properties

 <filters>
      <filter>src/main/resources/${env}.properties</filter>
</filters>

  然後是resources

  1. directory屬性指定資源文件放置的目錄。
  2. includes則是指定打包時,需要打到jar/war包裏的配置文件。下面例子是說需要把src/main/resources目錄下的所有的xml配置文件和init.properties屬性文件打到包裏,所以dev.properties,test.properties,product.properties這三個屬性文件不會出現在[jar/war]包文件裏
  3. filtering如果設置爲false的話,則表示上文的filters配置失效;如果設置爲true,則會根據${env}.properties裏面的鍵值對來填充includes指定文件裏的${xxxx}佔位符。
   <!-- 指定資源文件目錄 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 設置爲true,則init.properties會根據${env}.properties裏面的配置來填充 -->
            <filtering>true</filtering>
            <includes>
                <include>*.xml</include>
                <include>init.properties</include>
            </includes>
        </resource>
    </resources>

最後演示一下打包運行的結果,假設init.properties裏有這麼幾行:

###########################mysql#################################################
pool.driverClassName=${pool.driverClassName}
pool.url=${pool.url}
pool.username=${pool.username}
pool.password=${pool.password}
pool.maxWait=${pool.maxWait}
# env的屬性在這裏也有效
env=${env}

test.properties屬性文件裏是如下配置,注意沒有pool.maxWait配置

###########################mysql#################################################
pool.driverClassName=com.mysql.jdbc.Driver
pool.url=jdbc:mysql://192.168.100.23:3306/etmis
pool.username=root
pool.password=123456

則運行打包命令[mvn package -DskipTests -P test]之後,init.properties會被填充爲:

###########################mysql#################################################
pool.driverClassName=com.mysql.jdbc.Driver
pool.url=jdbc:mysql://192.168.100.23:3306/etmis
pool.username=root
pool.password=123456
pool.maxWait=${pool.maxWait}
# env的屬性在這裏也有效
env=test
pool.maxWait不會被填充,因爲在test.properties屬性文件裏面沒有這個鍵值對。在xml文件裏面的篩選填充也是一樣,會根據文件中的${xxxx}佔位符來篩選處理。
<profile>裏面配置的屬性和${env}.properties裏面的屬性如果有衝突,比如test.properties裏面也配置了【env=testaaaa】,會優先使用<profile>裏面的

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章