[英]Optional environment variables in Spring app
In my Spring Boot app's application.properties
I have this definition:在我的 Spring Boot 应用程序的
application.properties
我有这个定义:
someProp=${SOME_ENV_VARIABLE}
But this is an optional value only set in certain environments, I use it like this但这是一个仅在某些环境中设置的可选值,我是这样使用的
@Value("${someProp:#{null}}")
private String someProp;
Surprisingly I get this error when the env.令人惊讶的是,当 env. var doesn't exist
var 不存在
Could not resolve placeholder 'SOME_ENV_VARIABLE' in string value "${SOME_ENV_VARIABLE}"
I was expecting Spring to just set a blank value if not found in any PropertySource
.如果在任何
PropertySource
找不到,我希望 Spring 只设置一个空白值。
How to make it optional?如何使它成为可选的?
Provide a default value in the application.properties
在
application.properties
提供默认值
someProp=${SOME_ENV_VARIABLE:#{null}}
When used like @Value("${someProp})
, this will correctly evaluate to null
. First, if SOME_ENV_VARIABLE
is not found when application.properties
is being processed, its value becomes the string literal "#{null}". Then, @Value
evaluates someProp
as a SpEL expression, which results in null
. The actual value can be verified by looking at the property in the Environment
bean. 当像
@Value("${someProp})
这样使用时,它将正确评估为null
。首先,如果在处理application.properties
时未找到SOME_ENV_VARIABLE
,则其值将成为字符串文字“#{null}”。 @Value
someProp
评估为SpEL表达式,结果为null
。可以通过查看Environment
Bean中的属性来验证实际值。
This solution utilizes the default value syntax specified by the PlaceholderConfigurerSupport
class 此解决方案利用
PlaceholderConfigurerSupport
类指定的默认值语法
Default property values can be defined globally for each configurer instance via the properties property, or on a property-by-property basis using the default value separator which is ":" by default and customizable via setValueSeparator(String).
可以通过properties属性为每个配置程序实例全局定义默认属性值,也可以使用默认值分隔符(默认情况下为“:”并通过setValueSeparator(String)进行自定义)在逐个属性的基础上进行定义。
and Spring SpEL expression templating . 和Spring SpEL表达式模板 。
From Spring Boot docs on externalized configuration 来自Spring Boot docs关于外部化配置的信息
Finally, while you can write a SpEL expression in @Value, such expressions are not processed from Application property files.
最后,尽管您可以在@Value中编写SpEL表达式,但不会从Application属性文件中处理此类表达式。
Because this is probably interesting to others who come here, you can override any properties file w/ an env variable implicitly.因为这对来到这里的其他人来说可能很有趣,所以您可以隐式覆盖任何带有 env 变量的属性文件。 Let's say you have property.
假设你有财产。
someapp.foo
Then you can define an env variable SOMEAPP_FOO
(capital letters and . -> _ ) and spring will implicitly set the property from the env.然后你可以定义一个环境变量
SOMEAPP_FOO
(大写字母和 . -> _ ),spring 将从环境中隐式设置属性。 variable.多变的。
Described here此处描述
https://www.tutorialworks.com/spring-boot-kubernetes-override-properties/ https://www.tutorialworks.com/spring-boot-kubernetes-override-properties/
This work for me:这对我有用:
spring.datasource.url=jdbc:mysql://${DB_IP:localhost}:3306/app
spring.datasource.username=${SPRING_DATASOURCE_USERNAME:mylocaluser}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD:localpass}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.