Spring条件yaml属性值

Mic*_* Dz 0 java spring yaml properties spring-boot

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

profiles.active: rabbit-${CLUSTER_ENV}, mongo-${CLUSTER_ENV} ...
Run Code Online (Sandbox Code Playgroud)

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

if(CLUSTER_ENV == "local") {
   return "dev";
} else {
   return CLUSTER_ENV;
}
Run Code Online (Sandbox Code Playgroud)

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

WiP*_*iPU 6

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

 #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}}
Run Code Online (Sandbox Code Playgroud)

问候,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}}
Run Code Online (Sandbox Code Playgroud)