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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36294866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:40:23  来源:igfitidea点击:

spring boot: add new yml files to application config

javaspringspring-boot

提问by piotrek

i want developers to be able to locally override some configuration properties. (let's say we work on google drive and everyone should test it on its own account). i don't want to override properties using command line (because it has to be set inside every IDE configuration and on every CLI run).

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

what i want is: application should use all the standard spring boot config files (application.ymletc) and also look for e.g. local.yml(on the classpath) or some file inside user.home. and those additional files should override other settings.

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

how to add new ymlresources and order them correctly?

如何添加新yml资源并正确排序?

edit: i know spring's default orders and locations. question is about adding newones

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

回答by Ori Dar

From Spring Boot Documentation : Application property files:

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

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

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

  • A /config subdirectory of the current directory.
  • The current directory
  • A classpath /config package
  • The classpath root
  • 当前目录的 /config 子目录。
  • 当前目录
  • 一个类路径 /config 包
  • 类路径根

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

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

This also goes for yaml, so you everyone can add application.yml under configdirectory, under the directory you run the spring boot jar from.

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

You can also customize the extra configuration file to be local.ymlif you'd like by using spring.config.location:

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

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

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

Note however:

但请注意:

spring.config.name and spring.config.location are used very early to determine which files have to be loaded so they have to be defined as an environment property (typically OS env, system property or command line argument).

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

回答by dunni

If you look in the Spring Boot documentation about the locations for configuration files (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config), you can see, that they are loaded from the following places (amongst others):

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

  • Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  • Application properties outside of your packaged jar (application.properties and YAML variants).
  • 打包 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):

它们的加载位置有两个默认位置(参见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
  • 当前目录的 /config 子目录。
  • 当前目录

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.

本例中的当前目录是指 Java 进程的工作目录(通常是 JAR 所在的目录,或者在 IDE 中运行的情况下,通常是项目根文件夹)。因此,开发人员只需将他们自己的配置文件放在那些地方,它们就会被自动加载(并将覆盖 JAR 中的属性)。将这些文件添加到 .gitignore(或 .svnignore 或 ...),它们就不会意外提交到您的存储库中。

回答by Sai prateek

To provide the configuration from external config file in spring-boot application -

在 spring-boot 应用程序中提供来自外部配置文件的配置 -

-Dspring.config.location=file:/home/vfroot/Workspace/project/MODULE_HOME/application.yaml

this command can be run with terminal:

这个命令可以用终端运行:

mvn clean install -Dspring.config.location = file:/home/vfroot/Workspace/MODULE_HOME/application.yaml

mvn clean install -Dspring.config.location = file:/home/vfroot/Workspace/MODULE_HOME/application.yaml

or need to set in Eclipse VM argument.

或者需要在Eclipse中设置VM argument

Also to set the active profiles :

还要设置活动配置文件:

 -Dspring.profiles.active=dev

回答by Salim

Well, since i am new in Spring Boot & Restfull Web Services. However, i managed to add a new .yml file to mange database and server port.

好吧,因为我是 Spring Boot 和 Restfull Web 服务的新手。但是,我设法添加了一个新的 .yml 文件来管理数据库和服务器端口。

Instructions that i followed:

我遵循的说明:

  1. Project File.
  2. Other Sources
  3. src/main/resources
  4. default package
  5. right click on "default package"
  6. add new YAML FILE
  1. 项目文件。
  2. 其他来源
  3. 源代码/主要/资源
  4. 默认包
  5. 右键单击“默认包”
  6. 添加新的 YAML 文件

Or of YAML File option not available 5. right click on "default package" 6. then in categories: other --> File Types: YAML File

或者 YAML 文件选项不可用 5. 右键单击​​“默认包” 6. 然后在类别中:其他 --> 文件类型:YAML 文件