spring boot:将新的yml文件添加到应用程序配置中

pio*_*rek 6 java spring spring-boot

我希望开发人员能够在本地覆盖一些配置属性.(假设我们在google驱动器上工作,每个人都应该在自己的帐户上测试它).我不想使用命令行覆盖属性(因为它必须在每个IDE配置和每个CLI运行中设置).

我想要的是:应用程序应该使用所有标准的spring启动配置文件(application.yml等),并查找例如local.yml(在类路径上)或里面的一些文件user.home.这些附加文件应覆盖其他设置.

如何添加新yml资源并正确订购?

编辑:我知道春天的默认订单和位置.问题是关于添加

小智 6

在 Spring Boot v2.4 之后,有一种新方法可以使用spring.config.importhttps ://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4#导入附加配置

通过将此部分添加到您的application.yml文件中,您应该能够导入附加配置:

spring:
  config:
    import: local.yml
Run Code Online (Sandbox Code Playgroud)

文章还有这一段:

进口可以被视为插入在声明它们的文件下方的附加文件。它们遵循与常规多文档文件相同的自上而下的顺序:导入只会导入一次,无论声明多少次。

因此,应将 的内容local.yml处理为附加到 的末尾application.yml,从而允许您覆盖中的任何属性application.yml


dun*_*nni 5

如果您查看 Spring Boot 文档中有关配置文件位置的信息 ( http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config ),您可以看到,它们是从以下位置加载的(其中包括):

  • 打包 jar 之外的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)
  • 打包的 jar 之外的应用程序属性(application.properties 和 YAML 变体)。

There are two default locations where they are loaded from ( see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files):

  • A /config subdirectory of the current directory.
  • The current directory

Current directory in this case means the working directory for the Java process (Usually the directory where the JAR is located, or in case of running with in the IDE, usually the project root folder). So the developers just can place their own configuration files in that places and they are automatically loaded (and will override properties within the JARs). Add that files to .gitignore (or .svnignore or ...) and they won't accidentally committed into your repository.


Ori*_*Dar 0

来自Spring Boot 文档:应用程序属性文件

SpringApplication将从以下位置的application.properties文件加载属性并将它们添加到Spring环境中:

  • 当前目录的 /config 子目录。
  • 当前目录
  • 类路径 /config 包
  • 类路径根

该列表按优先级排序(列表中较高位置定义的属性优先于较低位置定义的属性)。

这也适用于 yaml,因此每个人都可以在config运行 spring boot jar 的目录下添加 application.yml 。

local.yml如果您愿意,您还可以使用以下命令自定义额外的配置文件spring.config.location

--spring.config.location=classpath:/application.yml,classpath:/local.yml

但请注意:

spring.config.name 和 spring.config.location 很早就被用来确定必须加载哪些文件,因此必须将它们定义为环境属性(通常是操作系统环境、系统属性或命令行参数)。

  • 并不是我“不能”。我只是“不想”。每个开发人员都必须为每个运行时配置、每个命令行调用设置它。我希望应用程序无需任何其他选项即可工作,并且它应该自动拾取现有的配置文件 (2认同)