Spring Boot 外置配置文件

默認(rèn)情況下,我們 spring boot 項(xiàng)目的配置文件<small>(application.yaml、application.properties)</small>是在項(xiàng)目的 jar 包『里面』的。

如果是要改配置文件中的配置項(xiàng)時(shí),就需要將項(xiàng)目重新打包,在某些情況下,這就顯得十分不方便。

對(duì)此,我們可以將 spring boot 項(xiàng)目的配置文件『挪到』jar 包之外,然后再啟動(dòng) spring boot 項(xiàng)目時(shí)再指定它使用外部的這些配置文件。

  • 為 pom.xml 添加插件及配置

    <plugin> <!-- copy資源文件 -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <id>copy-resources</id>
          <phase>package</phase>
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <configuration>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
                <includes>
                  <include>**/application*.yml</include>
                  <include>**/application*.properties</include>
                  <include>**/bootstrap.yml</include>
                  <include>**/logback*.xml</include>
                </includes>
                <excludes>
                  <exclude>**/i18n/**</exclude>
                  <exclude>**/mybatis/**</exclude>
                  <exclude>**/public/**</exclude>
                  <exclude>**/static/**</exclude>
                  <exclude>**/templates/**</exclude>
                </excludes>
              </resource>
            </resources>
            <outputDirectory>${project.build.directory}/resources</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <!-- 打jar包時(shí)忽略配置文件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.properties</exclude>
          <exclude>**/logback*.xml</exclude>
          <exclude>**/bootstrap.yml</exclude>
        </excludes>
      </configuration>
    </plugin>
    

根據(jù)上述的 <outputDirectory> 的配置,相關(guān)的配置文件會(huì)被復(fù)制到 target 下的 resources 目錄中,并且,jar 包中也不會(huì)包含你所配置的這些配置文件。

這種情況下,在啟動(dòng) spring boot 項(xiàng)目時(shí),需要額外的參數(shù)(-Dspring.config.location)告訴它項(xiàng)目的配置文件在哪:

java -jar -Dspring.config.location=E:/Workspace/projects/target/resources/ ./xxx.jar

注意:

  1. 如果外部文件指定的是目錄,那么 -Dspring.config.location 配置的地址必須以 / 結(jié)尾,表明這個(gè)配置是一個(gè)目錄,而非單個(gè)文件;

  2. 如果想要指定單個(gè)文件,可以使用 -Dspring.config.location=E:/Workspace/projects/target/resources/application.properties 這種方式來(lái)指明只需要加載這個(gè)配置文件內(nèi)容。

  3. 當(dāng)然,你可以可以將配置文件放在與啟動(dòng)的 jar 包同級(jí)目錄,或者在啟動(dòng)的jar的同級(jí)目錄下建一個(gè)名為 config 的目錄,然后將配置文件放到里面,然后直接使用 java -jar ./xxx.jar 命令啟動(dòng),spring 默認(rèn)會(huì)去 ./config 目錄中查詢。

補(bǔ)充

spring boot 默認(rèn)是以 classpath:/,classpath:/config/,file:./,file:./config/ 這樣的配置在查找、加載配置文件,有意思的是查找順序是上述配置的反向順序:

  1. file:./config/
  2. file:./
  3. classpath:/config/
  4. classpath:/

因此,如果你在 spring.config.location 中也定義了多個(gè)配置文件位置,例如:classpath:/custom-config/,file:./custom-config/, 那么配置文件的查找、加載順序同樣是反向的:

  1. file:./custom-config
  2. classpath:/custom-config

另外,還有一個(gè)功能相似的配置 spring.config.additional-location,使用它的話,它會(huì)作為默認(rèn)配置路徑的『擴(kuò)展配置』路徑來(lái)使用。擴(kuò)展的配置路徑會(huì)比默認(rèn)的配置優(yōu)先被掃描到. 比如說(shuō), 如果設(shè)置了擴(kuò)展的配置文件所在路徑為:classpath:/custom-config/,file:./custom-config/ , 那么查找路徑將會(huì)是下面的順序:

  1. file:./custom-config/
  2. classpath:custom-config/
  3. file:./config/
  4. file:./
  5. classpath:/config/
  6. classpath:/

這種掃描順序使得你可以通過(guò)自己的自定義配置來(lái)修改默認(rèn)的配置項(xiàng)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容