likes
comments
collection
share

自定义yml文件 读取属性为null(两种解决方法)

作者站长头像
站长
· 阅读数 25

读取models属性为null。

配置文件如下:

自定义yml文件 读取属性为null(两种解决方法)

对应类如下:

自定义yml文件 读取属性为null(两种解决方法)

使用了@PropertySource 指定classpath:application-specialness.yml,并没有起作用。

一、

通过springboot2中@ConfigurationProperties装载yml文件的时候调取出现值为null的解决办法springboot 加载自定义yml文件(@PropertySource)了解到,@PropertySource默认不支持yml文件,需要新建类,继承DefaultPropertySourceFactory,重写createPropertySource方法。

自定义yml文件 读取属性为null(两种解决方法)

二、

除此之外,还有一种方法。

配置spring.profiles.include属性

自定义yml文件 读取属性为null(两种解决方法)

这样就可以直接读取到属性。

参考:

1、springboot 加载自定义yml文件(@PropertySource) 2、springboot2中@ConfigurationProperties装载yml文件的时候调取出现值为null的解决办法 3、springboot中spring.profiles.include的妙用