java 使用两个 yaml 文件进行配置属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39921174/
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-11-03 04:45:51  来源:igfitidea点击:

Using two yaml files for configuration properties

javaspring-bootpropertiesconfigurationyaml

提问by user2296988

We are using a spring boot application, where properties are loaded from application.ymlfile instead of application.properties, located at src/main/resources/which looks like below:

我们正在使用一个 Spring Boot 应用程序,其中的属性是从application.yml文件而不是 ,加载 的application.properties,其位置src/main/resources/如下所示:

config: 
  host: localhost:8080  
  server: 123  

And they are being pulled in a .javafile like this

他们被拉入这样的.java文件

@ConfigurationProperties( prefix="config")  
public class ConnectionImpl implements Connection{
  @Value("${config.host}")
  private Stringhost;
} 

I am able to retrieve properties this way. But we are trying to move the config properties from application.ymlto a different .ymlfile which is located at a different location. (src/main/resources/env-config).
Now I am not able to retrieve properties same way, i.e, using @Valueannotation. Is there any other annotation I need to add ?

我能够以这种方式检索属性。但是我们正在尝试将配置属性从位于不同位置application.yml的不同.yml文件中移动。( src/main/resources/env-config).
现在我无法以相同的方式检索属性,即使用@Value注释。我需要添加任何其他注释吗?

回答by su45

From the documentation:

文档

SpringApplicationwill load properties from application.properties(or application.yml) files in the following locations and add them to the Spring Environment:

  1. A /configsubdirectory of the current directory.
  2. The current directory
  3. A classpath /configpackage
  4. The class path root

If you don't like application.propertiesas the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.locationenvironment property (comma-separated list of directory locations, or file paths).

The default search path classpath:,classpath:/config,file:,file:config/is always used, irrespective of the value of spring.config.location. This search path is ordered from lowest to highest precedence (file:config/wins). If you do specify your own locations, they take precedence over all of the default locations and use the same lowest to highest precedence ordering. In that way you can set up default values for your application in application.properties(or whatever other basename you choose with spring.config.name) and override it at runtime with a different file, keeping the defaults.

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

  1. 一个/config当前目录下的子目录。
  2. 当前目录
  3. 一个类路径/config
  4. 类路径根

如果您不喜欢application.properties作为配置文件名,您可以通过指定 spring.config.name 环境属性切换到另一个。您还可以使用spring.config.location环境属性(以逗号分隔的目录位置列表或文件路径)引用显式位置

classpath:,classpath:/config,file:,file:config/无论 的值如何,始终使用默认搜索路径spring.config.location。此搜索路径按从低到高的优先级排序(file:config/wins)。如果您确实指定了自己的位置,则它们优先于所有默认位置并使用相同的从最低到最高的优先顺序。通过这种方式,您可以为您的应用程序application.properties(或您选择的任何其他基本名称spring.config.name)设置默认值,并在运行时使用不同的文件覆盖它,保持默认值。

You need to supply a command line argument that tells SpringApplicationwhere specifically to look. If everything in resources/is added to the classpath root, then your command line would look like:

您需要提供一个命令行参数,告诉您SpringApplication具体要查找的位置。如果所有内容resources/都添加到类路径根目录中,那么您的命令行将如下所示:

java -jar myproject.jar --Dspring.config.location=classpath:/env-config/service-config.yml

java -jar myproject.jar --Dspring.config.location=classpath:/env-config/service-config.yml

If you have a general application.ymlunder resources/, the properties in there will still be loaded but will take a lower precedence to the properties file specified on the command line.

如果您有一个通用application.ymlunder resources/,则仍然会加载其中的属性,但将优先于命令行上指定的属性文件。

回答by Michael Lihs

Your question doesn't really say what you intend to do, but if you want to have a different configuration for different environments (e.g. development, test, production), there is a simple solution for that.

您的问题并没有真正说明您打算做什么,但是如果您想针对不同的环境(例如developmenttestproduction)使用不同的配置,则有一个简单的解决方案。

Place your config files in a file hierarchy like this inside your project:

将您的配置文件放在项目中这样的文件层次结构中:

src/
  main/
    resources/
      application.yml
      application-development.yml
      application-test.yml
      application-production.yml

When you now start your application with

当你现在开始你的应用程序时

java -jar mySpringApplication.jar -Dspring.profiles.active=development

the configuration from application.ymlwill be taken as a "base layer", overridden by the configuration in application-development.yml. By this, you can have "default" settings for all environments in application.ymland environment-specific configuration in the application-ENV.ymlfiles. The same works for testand production.

中的配置application.yml将被视为“基础层”,被 中的配置覆盖application-development.yml。通过这种方式,您可以为文件中的所有环境application.yml和特定于环境的配置设置“默认”设置application-ENV.yml。同样适用于testproduction

回答by Stephane Nicoll

No.

不。

You'll be in a much better position if you avoid hard-coding file path like that within your code base. @ConfigurationPropertiesused to have a locationsattribute but it's deprecated and already removed in 1.5.

如果您避免在代码库中使用类似的硬编码文件路径,您将处于一个更好的位置。@ConfigurationProperties曾经有一个locations属性,但它已被弃用并已在 1.5 中删除。

In Spring Boot, you configure the Environmentwhich is a single source of truth for your configuration. Rather than having settings buried in code, you should configure Spring Boot to read the files that you want. Read the documentation for spring.config.location. If you want to do this in a more transparent manner, perhaps EnvironmentPostProcessoris what you need

在 Spring Boot 中,您配置了Environment这是您的配置的单一真实来源。您应该配置 Spring Boot 以读取所需的文件,而不是将设置隐藏在代码中。阅读 的文档spring.config.location。如果您想以更透明的方式执行此操作,也许这EnvironmentPostProcessor就是您所需要的