在Spring Boot中转义Yaml中的Map键中的一个点

the*_*dam 26 spring yaml key spring-boot

我有一个以下的yml配置:

foo:
  bar.com:
    a: b
  baz.com:
    a: c
Run Code Online (Sandbox Code Playgroud)

使用以下类,Spring尝试使用键"bar"和"baz"注入地图,将点视为分隔符:

public class JavaBean {
    private Map<String, AnotherBean> foo;
(...)
}
Run Code Online (Sandbox Code Playgroud)

我试过引用密钥(即'bar.com'或"bar.com"),但无济于事 - 仍然是同样的问题.有没有解决的办法?

pio*_*oto 55

@ fivetenwill的答案略有修改,适用于Spring Boot 1.4.3.RELEASE:

foo:
  "[bar.com]":
    a: b
  "[baz.com]":
    a: c
Run Code Online (Sandbox Code Playgroud)

你需要括号在引号内,否则YAML解析器在它们到达Spring之前基本上丢弃它们,并且它们不会将它放入属性名称中.


小智 5

这应该有效:

foo:
  "[bar.com]":
    a: b
  "[baz.com]":
    a: c
Run Code Online (Sandbox Code Playgroud)

灵感来自Spring Boot 配置绑定Wiki