Spring Boot 外置配置文件

默认情况下,我们 spring boot 项目的配置文件<small>(application.yaml、application.properties)</small>是在项目的 jar 包『里面』的。

如果是要改配置文件中的配置项时,就需要将项目重新打包,在某些情况下,这就显得十分不方便。

对此,我们可以将 spring boot 项目的配置文件『挪到』jar 包之外,然后再启动 spring boot 项目时再指定它使用外部的这些配置文件。

  • 为 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包时忽略配置文件 -->
    <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>
    

根据上述的 <outputDirectory> 的配置,相关的配置文件会被复制到 target 下的 resources 目录中,并且,jar 包中也不会包含你所配置的这些配置文件。

这种情况下,在启动 spring boot 项目时,需要额外的参数(-Dspring.config.location)告诉它项目的配置文件在哪:

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

注意:

  1. 如果外部文件指定的是目录,那么 -Dspring.config.location 配置的地址必须以 / 结尾,表明这个配置是一个目录,而非单个文件;

  2. 如果想要指定单个文件,可以使用 -Dspring.config.location=E:/Workspace/projects/target/resources/application.properties 这种方式来指明只需要加载这个配置文件内容。

  3. 当然,你可以可以将配置文件放在与启动的 jar 包同级目录,或者在启动的jar的同级目录下建一个名为 config 的目录,然后将配置文件放到里面,然后直接使用 java -jar ./xxx.jar 命令启动,spring 默认会去 ./config 目录中查询。

补充

spring boot 默认是以 classpath:/,classpath:/config/,file:./,file:./config/ 这样的配置在查找、加载配置文件,有意思的是查找顺序是上述配置的反向顺序:

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

因此,如果你在 spring.config.location 中也定义了多个配置文件位置,例如:classpath:/custom-config/,file:./custom-config/, 那么配置文件的查找、加载顺序同样是反向的:

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

另外,还有一个功能相似的配置 spring.config.additional-location,使用它的话,它会作为默认配置路径的『扩展配置』路径来使用。扩展的配置路径会比默认的配置优先被扫描到. 比如说, 如果设置了扩展的配置文件所在路径为:classpath:/custom-config/,file:./custom-config/ , 那么查找路径将会是下面的顺序:

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

这种扫描顺序使得你可以通过自己的自定义配置来修改默认的配置项。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容