Spring Boot读取YAML中Map格式配置失败 咨询YamlMapFactoryBean实现方案
解决方案
方案一:@ConfigurationProperties + 自定义YamlPropertySourceFactory(推荐)
你的YamlPropertySourceFactory实现本身是正确的,报错的核心原因是:@Value注解不支持直接注入整个Map结构,要注入集合类型的配置必须配合@ConfigurationProperties使用,修改步骤如下:
- 确认
order_config.yml结构和代码的配置前缀对应,示例正确结构:
yaml: mappings: name: "foo" aliases: "abc"
- 修改
ValidatorConfig配置类,放开@ConfigurationProperties注解,补充getter/setter方法:
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.util.Map; @Configuration @ConfigurationProperties(prefix = "yaml") @PropertySource(value = "classpath:order_config.yml", factory = YamlPropertySourceFactory.class) public class ValidatorConfig { // 字段名和配置里的key完全对应 private Map<String, String> mappings; // 单个属性可以继续用@Value注入 @Value("${yaml.mappings.name}") private String create; // 必须添加public的getter、setter,@ConfigurationProperties靠反射调用setter完成注入 public Map<String, String> getMappings() { return mappings; } public void setMappings(Map<String, String> mappings) { this.mappings = mappings; } public String getCreate() { return create; } public void setCreate(String create) { this.create = create; } public String getValidatorBean(String tenant, String requestType) { System.out.println(mappings); return "yes"; } }
- 在项目启动类添加
@EnableConfigurationProperties注解开启配置绑定能力:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
方案二:使用YamlMapFactoryBean实现
如果你需要灵活读取整个YAML内容、或者配置结构不固定,可以使用YamlMapFactoryBean直接将整个YAML文件解析为Map注入,不需要自定义PropertySourceFactory,实现代码如下:
- 定义YAML解析Bean:
import org.springframework.beans.factory.config.YamlMapFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import java.util.Map; @Configuration public class YamlMapConfig { @Bean public Map<String, Object> orderConfigMap() { YamlMapFactoryBean factoryBean = new YamlMapFactoryBean(); // 指定要读取的YAML文件路径 factoryBean.setResources(new ClassPathResource("order_config.yml")); return factoryBean.getObject(); } }
- 在需要使用配置的地方直接注入该Map即可:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; @Component public class TestService { @Autowired private Map<String, Object> orderConfigMap; public void test() { // 按层级读取配置 Map<String, Object> yamlConfig = (Map<String, Object>) orderConfigMap.get("yaml"); Map<String, String> mappings = (Map<String, String>) yamlConfig.get("mappings"); System.out.println(mappings.get("name")); // 输出foo } }
常见问题排查
- 配置文件的key层级、拼写、大小写必须和代码里的配置前缀、字段名完全匹配
- @ConfigurationProperties修饰的类必须包含public类型的getter、setter方法
- 启动类必须添加@EnableConfigurationProperties注解才能生效
内容的提问来源于stack exchange,提问作者Raghav Mittal
相关产品推荐
相关产品推荐

