在运行时设置/覆盖 Spring / Spring Boot 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27919270/
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
Set/override Spring / Spring Boot properties at runtime
提问by ps-aux
At the project with Spring Boot we use application.properties but need to configure some of these properties (like port number of logging level) based on an external configuration. We access the configuration via API so it is known only at runtime.
在带有 Spring Boot 的项目中,我们使用 application.properties 但需要根据外部配置配置其中一些属性(如日志级别的端口号)。我们通过 API 访问配置,因此只有在运行时才能知道。
Is there a way to override or set some Spring properties at runtime (for example using a bean) and if yes how can this be achieved?
有没有办法在运行时覆盖或设置一些 Spring 属性(例如使用 bean),如果是,如何实现?
回答by Todderz
You could do this with Spring Cloud Config
你可以用Spring Cloud Config做到这一点
Just for the purpose of illustration, here's a relatively quick way to see dynamic property overrides at runtime:
仅出于说明目的,这里有一种在运行时查看动态属性覆盖的相对快速的方法:
First, for your bean to be able to pick up changed properties, you need to annotate it with
首先,为了让您的 bean 能够获取更改的属性,您需要对其进行注释
@RefreshScope
Add the spring cloud dependency to your spring boot app, eg for gradle
将 spring cloud 依赖项添加到您的 spring boot 应用程序中,例如用于 gradle
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter', version: '1.1.1.RELEASE'
( NB You also need the spring boot actuator dependency.)
(注意,您还需要弹簧引导执行器依赖项。)
With the app running, you can view your current config at eg
随着应用程序的运行,您可以在例如查看您当前的配置
http://localhost:8080/env
eg if you have a property 'my.property' in application.properties, you'll see something like:
例如,如果您在 application.properties 中有一个属性“my.property”,您将看到如下内容:
"applicationConfig: [classpath:/application.properties]": {
"my.property": "value1",
etc
To change the value, POST my.property=value2 to /env as application/x-www-form-urlencoded
要更改该值,请将 my.property=value2 作为 application/x-www-form-urlencoded 发布到 /env
eg
例如
curl -X POST http://localhost:8080 -d my.property=value2
GET /env again and you'll see the new value appears under the "manager" section
再次 GET /env ,您将看到新值出现在“manager”部分下
To apply the changed properties, do an empty POST to /refresh. Now your bean will have the new value.
要应用更改的属性,请对 /refresh 执行空的 POST。现在您的 bean 将具有新值。
回答by rhinds
Could you use system properties to pass in the variable? If you configure the PropertyPlaceholderConfigurer
you can set the precedence of system properties vs file properties.
您可以使用系统属性来传递变量吗?如果您配置了,PropertyPlaceholderConfigurer
您可以设置系统属性与文件属性的优先级。
For example, something like:
例如,类似于:
@Bean public PropertyPlaceholderConfigurer placeHolderConfigurer() {
PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer()
props.setSystemPropertiesMode( PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE )
props.setLocations(new
PathMatchingResourcePatternResolver().getResources("classpath:/**.properties"));
props
}
The above would load your .properties file, but we set the priority to be system variables first, so if you set a system variable that will override the same variable in the config.
以上将加载您的 .properties 文件,但我们首先将优先级设置为系统变量,因此如果您设置一个系统变量将覆盖配置中的相同变量。
Alternatively, looking at the docs, Spring recommends defining a search order in your Environment:
或者,查看docs,Spring 建议在您的环境中定义搜索顺序:
[PropertyPlaceholderConfigurer is still appropriate for use when] existing configuration makes use of the "systemPropertiesMode" and/or "systemPropertiesModeName" properties. Users are encouraged to move away from using these settings, and rather configure property source search order through the container's Environment; however, exact preservation of functionality may be maintained by continuing to use PropertyPlaceholderConfigurer.
[PropertyPlaceholderConfigurer 仍然适用于] 现有配置使用“systemPropertiesMode”和/或“systemPropertiesModeName”属性。鼓励用户不要使用这些设置,而是通过容器的环境配置属性源搜索顺序;然而,可以通过继续使用 PropertyPlaceholderConfigurer 来保持功能的精确保留。
Hopefully one of the above should sort out what you need?
希望以上之一应该可以解决您的需求?