@PropertySource加载yml
1、yml文件通常是 application.yml
当我们需要新的yml文件时,新建一个yml文件 response.yml
response:name: 张三
2、启动类新加
@PropertySource(value = {"classpath:response.yml"}, encoding = "utf-8", factory = ResponseFactory.class)
3、新增工厂类
public class ResponseFactory extends DefaultPropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {if (resource == null){return super.createPropertySource(name, resource);}List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());return sources.get(0);}}
4、新建Response类
@Component@Datapublic class Response {@Value("${response.name}")private String name;}
5、测试
@Testpublic void test() {try{System.out.println("666555:"+response.getName());} catch (Exception e) {}}
控制台:666555:张三
赞 (0)
