我参考了Spring Boot - 从 application.yml注入映射来从 application.yml 文件注入映射我的 application.yml 片段如下easy.app.pairMap: test1: 'value1' test2: 'value2'属性文件如下@Component@Configuration@ConfigurationProperties("easy.app")@EnableConfigurationPropertiespublic class TestProperties {private Map<String, String> pairMap= new HashMap<String, String>();public void setPairMap(Map<String, String> pairMap) { this.pairMap= pairMap;}}但是,我发现只有当 setter 和 getter 的格式正确时才会发生值注入。即 getPairMap 和 setPairMap。不是在使用 getPairs 或 SetPairs 时。这种行为的原因是什么
2 回答

斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
Spring通过前缀easy.app.pairMap
查找您的属性全名,然后尝试使用 name 查找 setter ,它获取属性 name并将其“转换”为.ConfigurationProperties
easy.app
setPairMap
pairMap
setPairMap
如果创建方法setPairs
属性名称应该是easy.app.pairs
.

侃侃尔雅
TA贡献1801条经验 获得超16个赞
要使用 Spring Boot 的Binder
实用程序绑定到属性(就是这样@ConfigurationProperties
做的),您需要在目标 bean 中有一个属性,并且您需要提供一个 setter 或使用可变值对其进行初始化。
Spring如何理解它需要使用SetPairs
方法来设置你的pairMap
属性?getter 和 setter 的命名有约定,如果您希望一切正常,则应遵循此约定。
添加回答
举报
0/150
提交
取消