2017-06-14 80 views
3

从弹簧启动yaml文件绑定地图与复杂的关键字我试图从一个复杂的密钥从spring yaml配置文件到使用弹簧启动和@ConfigurationProperties注释解组映射。有许多关于用简单的按键映射的例子,像无法使用@ConfigurationProperties

map: 
    key: value 

,甚至一个简单的关键和复杂的值映射,像

map: 
    key: {firstPartOfComplexValue: alpha, secondPartOfComplexValue: beta} 

我测试过两个以上的例子 - 做工不错。

现在我需要在地图中的复杂的关键:

map: 
    ? {firstPartOfAKey: someValue1, secondPartOfAKey: someValue2}: value 

而这样解组的结果是一个空的地图。 可否请你,指点我,我在做什么错 预先感谢

有我的代码:

application.yml

custom: 
    users: 
    ? {firstPartOfAKey: hello, secondPartOfAKey: world} : tom 

豆来解读

@Component 
@ConfigurationProperties("custom") 
    public class MyBean { 
    private Map<Key, String> users = new HashMap<>(); 

    public Map<Key, String> getUsers() { 
     return users; 
    } 

    public void setUsers(Map<Key, String> users) { 
     this.users = users; 
    } 

    @Override 
    public String toString() { 
     return users.toString(); 
    } 

    public static class Key { 
     private String firstPartOfAKey; 
     private String secondPartOfAKey; 

     @Override 
     public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 
     Key key = (Key) o; 
     return Objects.equals(firstPartOfAKey, key.firstPartOfAKey) && 
       Objects.equals(secondPartOfAKey, key.secondPartOfAKey); 
     } 

     @Override 
     public int hashCode() { 
     return Objects.hash(firstPartOfAKey, secondPartOfAKey); 
     } 

     public String getFirstPartOfAKey() { 
     return firstPartOfAKey; 
     } 

     public void setFirstPartOfAKey(String firstPartOfAKey) { 
     this.firstPartOfAKey = firstPartOfAKey; 
     } 

     public String getSecondPartOfAKey() { 
     return secondPartOfAKey; 
     } 

     public void setSecondPartOfAKey(String secondPartOfAKey) { 
     this.secondPartOfAKey = secondPartOfAKey; 
     } 

     @Override 
     public String toString() { 
     return String.format("firsPartOfKey: '%s', secondPartOfKey: '%s'", firstPartOfAKey, secondPartOfAKey); 
     } 
    } 
} 

的java配置(它是空的)

@Configuration 
@ComponentScan(basePackages = {"com"}) 
@EnableAutoConfiguration 
@EnableConfigurationProperties 
public class Config { 
} 

单元测试

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = {Config.class}) 
public class TestProps { 

    @Autowired 
    private MyBean myBean; 

    @Test 
    public void testYamlPropsLoad() { 
     System.out.println(myBean); 
    } 
} 

测试打印 '{}' 在地图只有复杂的密钥。其他地图(简单的键)工作良好。

回答

1

似乎结合您MyBean在所有如果没有任何异常,执行你的测试没有被处理,通常应该有类似的异常:

与靶{}失败:

属性:custom.null值:{{firstPartOfAKey = hello, secondPartOfAKey = world} = tom}原因:无法将类型为'java.lang.String'的属性值 转换为所需的类型'com.example.MyBean $ Key' 属性'null';嵌套的例外是 java.lang.IllegalStateException:无法将类型的价值 “java.lang.String中”所需类型“com.example.MyBean $钥匙”:没有 匹配编辑或转换战略发现

显然,解析器无法处理复杂的键,并且正在解释键null和值{firstPartOfAKey=hello, secondPartOfAKey=world}=tom

也许你可以通过执行custom editor/converter来找到另一种处理配置的方法。

+0

感谢您的回答。没有任何异常(甚至没有任何警告),只是空的地图。但是当我在yaml文件和MyBean中交换了key和value(所以key变成了String并且value变成了复杂类型)之后,一切正常。看来这是springboot中的一个错误。 Yaml文件被在线验证器检查,它是正确的。我会尝试通过建议的自定义编辑器/转换器来解决这个问题。谢谢。 – shaolin

+0

不客气。我不知道这是否是一个错误,或者它是否意图不支持复杂的键,并且奇怪你的情况没有任何豁免。尽管如此,尝试去定制转换器。 :) –