java - Spring条件yaml属性值

标签 java spring spring-boot properties yaml

这就是我的 .yaml 属性文件中的一行的样子:

profiles.active: rabbit-${CLUSTER_ENV}, mongo-${CLUSTER_ENV} ...

我只想将以下逻辑仅用于 rabbit- 属性:

if(CLUSTER_ENV == "local") {
   return "dev";
} else {
   return CLUSTER_ENV;
}

其他属性应该用local填充,但只有在这个地方,属性值才应该有条件地填充。我可以以某种方式在 Spring yaml 属性中添加此逻辑吗?

最佳答案

它看起来不太漂亮,但你可以使用类似的东西:

 #this can be added on startup
mykey: key1

#a map with your condition
mymap:
  key1: val1
  key2: val2

#your value based on the condition
conditional: con-${mymap.${mykey}}

问候, 威普

根据评论更新:

 #this can be added on startup as variable
mykey: local

#a map with your condition
mymap:
  local: dev
  xyz: test

# your value based on the condition or the key as fallback if the key is not 
# present in mymap.
conditional: con-${mymap.${mykey}:${mykey}}

关于java - Spring条件yaml属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321412/

相关文章:

java - 如何在intellij上安装ibatis插件

java - 自定义标签无法正常工作

java - Spring 集成: Unit test Start/Stop endpoint adapters

spring-boot - spring boot test中如何配置HandlerMethodArgumentResolver

java - Android 手机 radio 类型的值是否始终为 GSM、LTE、CDMA 或 WCDMA?

java - 通过Java应用程序动态更新和生成WAR文件

java.lang.IllegalStateException : Autowired annotation requires at least one argument 错误

java - 无法使用 Spring Security 消除弹出的用户凭据请求?

spring-boot - 如何在 spring-boot 配置中加载 application.yaml 配置以进行 selenium 测试

java - 仅加热 URI,不等待 Spring Rest 中的响应