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
Using two yaml files for configuration properties
提问by user2296988
We are using a spring boot application, where properties are loaded from application.yml
file 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 .java
file 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.yml
to a different .yml
file 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 @Value
annotation. Is there any other annotation I need to add ?
我能够以这种方式检索属性。但是我们正在尝试将配置属性从位于不同位置application.yml
的不同.yml
文件中移动。( src/main/resources/env-config
).
现在我无法以相同的方式检索属性,即使用@Value
注释。我需要添加任何其他注释吗?
回答by su45
From the documentation:
从文档:
SpringApplication
will load properties fromapplication.properties
(orapplication.yml
) files in the following locations and add them to the SpringEnvironment
:
- A
/config
subdirectory of the current directory.- The current directory
- A classpath
/config
package- The class path root
If you don't like
application.properties
as 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 thespring.config.location
environment 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 ofspring.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 inapplication.properties
(or whatever other basename you choose withspring.config.name
) and override it at runtime with a different file, keeping the defaults.
SpringApplication
将从以下位置的application.properties
(或application.yml
)文件加载属性并将它们添加到 SpringEnvironment
:
- 一个
/config
当前目录下的子目录。- 当前目录
- 一个类路径
/config
包- 类路径根
如果您不喜欢
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 SpringApplication
where 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.yml
under 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.yml
under 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.
您的问题并没有真正说明您打算做什么,但是如果您想针对不同的环境(例如development
、test
、production
)使用不同的配置,则有一个简单的解决方案。
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.yml
will 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.yml
and environment-specific configuration in the application-ENV.yml
files. The same works for test
and production
.
中的配置application.yml
将被视为“基础层”,被 中的配置覆盖application-development.yml
。通过这种方式,您可以为文件中的所有环境application.yml
和特定于环境的配置设置“默认”设置application-ENV.yml
。同样适用于test
和production
。
回答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. @ConfigurationProperties
used to have a locations
attribute but it's deprecated and already removed in 1.5.
如果您避免在代码库中使用类似的硬编码文件路径,您将处于一个更好的位置。@ConfigurationProperties
曾经有一个locations
属性,但它已被弃用并已在 1.5 中删除。
In Spring Boot, you configure the Environment
which 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 EnvironmentPostProcessor
is what you need
在 Spring Boot 中,您配置了Environment
这是您的配置的单一真实来源。您应该配置 Spring Boot 以读取所需的文件,而不是将设置隐藏在代码中。阅读 的文档spring.config.location
。如果您想以更透明的方式执行此操作,也许这EnvironmentPostProcessor
就是您所需要的