二、PropertySource支持指定yml文件的方法
默认PropertySource只支持properties文件,若也想支持yml文件,重写一个类,让其同时支持这两种格式的文件
public class CommPropertyResourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException { String resourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename()); if (resourceName.endsWith(".yml") || resourceName.endsWith(".yaml")) { List<PropertySource<?>> yamlSources = new YamlPropertySourceLoader().load(resourceName, resource.getResource()); return yamlSources.get(0); } else { return new DefaultPropertySourceFactory().createPropertySource(name, resource); } } }
1、@ConfigrualtionProperties(prefix="person")
application.properties 和application.yaml 是全局配置文件,若将所有的配置文件,都放到里边,信息会多。
2、@PropertySource(value={"classpath:person.propertyies"}) 可以指定配置文件
备注:PropertySource只能导入properties文件,不能导入yml文件。
3、将类属性配置到配置文件中,使用@value 取值