tips @PropertiesSource


在 spring 中使用 AnnotationConfigServletWebServerApplicationContext 时用 @PropertySource 注入绑定默认只支持xml或properties,
如果配置文件是 yaml 的需要加上工厂属性 factory = YamlPropertySourceFactory.class

点击查看代码
public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        //没有传入名称则取文件名
        String sourceName = name != null ? name:resource.getResource().getFilename();
        //新建 FactoryBean 解析配置文件
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        //返回解析结果
        return new PropertiesPropertySource(sourceName,factory.getObject());
    }
}