Spring条件Yaml属性值

时间:2019-05-27 07:19:25

标签: 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属性中添加此逻辑吗?

1 个答案:

答案 0 :(得分:0)

它看起来不漂亮,但是您可以使用类似以下内容的东西:

 #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}}

致谢, WiPU

基于评论的更新:

 #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}}