读取yaml文件配置信息
yaml文件配置
model-type:
config-map: {"422000011":"01","422000012":"02","422000013":"03","422000014":"04","422000015":"05","422000016":"06","422000017":"07","422000018":"08"}
event-type-list:
- eventType: "202400001"
eventTypeName: "1111"
aimmsEventType: "1"
- eventType: "202400001"
eventTypeName: "1111"
aimmsEventType: "1"
test-dd: "1234"
类文件
@PropertySource(value = {"classpath:application-static.yaml"},encoding = "utf-8",factory = YamlPropertyLoaderFactory.class) @EnableConfigurationProperties(ModelTypeConfig.class) @ConfigurationProperties(prefix = "model-type") @Configuration @Data public class ModelTypeConfig{ @ApiModelProperty(“") //@Value("${configMap}") private JSONObject configMap; @ApiModelProperty("") //@Value("${eventTypeList}") private List<EventTypeConfigNewDto> eventTypeList; private String testDd; @Data public static class EventTypeConfigNewDto { private String eventType; private String eventTypeName; private String aimmsEventType; } }
遇到问题:ModelTypeConfig里的所有字段值都为null,只有加上@Value才可以读到值
问题原因:当前springboot版本里面@ConfigurationProperties默认配置properties文件,而暂时不支持默认导入yml文件(以上yml文件改用properties文件导入是可行不会出现null)
解决方案:通过重写框架默认DefaultPropertySourceFactory方法,使.yml文件装配成功
public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { if (resource == null){ return super.createPropertySource(name, resource); } return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource()).get(0); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具