版权声明:本文为博主原创文章,未经博主允许不得转载。
@PropertySource:加载指定的配置文件; 因为当我们把所有配置文件都放在全局的配置文件 中时会导致配置文件过多,所以我们可以根据业务逻辑把配置文件分开来放
本次做了一个测试,当自定义配置文件后缀为yml时会导致注入失败,下面请看测试结果
配置文件 persion.yml
name: zhangsan
age: 20
isBoss: false
birth: 2018/09/03
maps: { k1: v1 ,k2: v2}
lists: [1,2,3]
dog:
name: dog
age: 10
bean
@Component
@PropertySource(value = "classpath:persion.yml")
@ConfigurationProperties(prefix = "persion" )
public class Persion {
private String name;
private Integer age;
private boolean isBoss;
private Date birth;
private String lastName;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//get set
}
测试结果
Persion{name='null', age=null, isBoss=false, birth=null, maps=null, lists=null, dog=null}
配置文件 persion.properties
# 配置persion
# idea 默认UTF-8
persion.name=张三
persion.age=18
persion.birth=2018/02/11
persion.isBoss=true
persion.maps.k1=v1
persion.maps.k2=v2
persion.lists=1,2,3
persion.dog.name=dog
persion.dog.age=15
persion.last-name=王五
bean
@Component
@PropertySource(value = "classpath:persion.properties")
@ConfigurationProperties(prefix = "persion" )
public class Persion {
private String name;
private Integer age;
private boolean isBoss;
private Date birth;
private String lastName;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//get set
}
测试结果
Persion{name='张三', age=18, isBoss=true, birth=Sun Feb 11 00:00:00 CST 2018, maps={k2=v2, k1=v1}, lists=[1, 2, 3], dog=Dog{name='dog', age=15}}
自定义配置文件目前只支持properties方式