yml文件来增加新的属性配置,新增属性放在application.yml中是没问题的,但是放其他文件中,然后通过@PropertySource 引入时,却出现了问题,所有.yml中的参数配置全部读取无效,properties文件是正常的,后来在stackoverflow上看到@PropertySource中存在factory参数,通过配置factory参数可以达到我们想要的效果。
@PropertySource factory属性的factory默认配置是Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;, 我们再看下PropertySourceFactory的源码就可知道了,
默认实现DefaultPropertySourceFactory 只支持xml和properties
所以需要自己拓展支持yml:
==========================================简单混合支持.yml ,.properties=
package com.sty.manager.properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
/**
* Created by Administrator on 2021/10/12
*/
public class MixPropertySourceFactory extends DefaultPropertySourceFactory {
private static final String YML_FILE_EXTENSION = ".yml";
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if ( !resource.getResource().exists() ){
return new PropertiesPropertySource(sourceName,new Properties());
}else if ( sourceName != null && (sourceName.endsWith(YML_FILE_EXTENSION) || sourceName.endsWith("yaml")) ){
Properties propertiesFromYaml = loadYamlProperties(resource);
return new PropertiesPropertySource(sourceName,propertiesFromYaml);
}
return super.createPropertySource(name, resource);
}
private Properties loadYamlProperties(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
mix1.yml
mix:
name: 混合
sex: 未知
high: 170
mix2.properties
mix.age = 18
mix.car = 红旗汽车
mix.color = red
javabean
/**
* Created by Administrator on 2021/10/12
*/
@PropertySource(value = {"classpath:mix1.yml","classpath:mix2.properties"},encoding = "UTF-8",factory = MixPropertySourceFactory.class)
@Component
@ConfigurationProperties(prefix = "mix")
public class MixVo implements Serializable {
private String name;
private String sex;
private Integer high;
private int age;
private String car;
private String color;
public MixVo() {
}
public MixVo(String name, String sex, Integer high, int age, String car, String color) {
this.name = name;
this.sex = sex;
this.high = high;
this.age = age;
this.car = car;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getHigh() {
return high;
}
public void setHigh(Integer high) {
this.high = high;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "MixVo{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", high=" + high +
", age=" + age +
", car='" + car + '\'' +
", color='" + color + '\'' +
'}';
}
}
================================单一支持yml
实现接口PropertySourceFactory
/**
* Created by Administrator on 2021/10/12
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlProperties(resource);
String sourceName = name != null? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName,propertiesFromYaml);
}
private Properties loadYamlProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
javabean
/**
* Created by Administrator on 2021/10/12
*/
@PropertySource( value = "classpath:person.yml",encoding = "UTF-8",factory = YamlPropertySourceFactory.class)
//@PropertySource( value = "classpath:person.properties",encoding = "UTF-8")
@Component
@ConfigurationProperties(prefix = "person")
public class PersonVo {
private String name;
private Integer age;
public PersonVo() {
}
public PersonVo(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
person.yml
person:
name: 丽莎
age: 18