第四节,Springboot加载指定配置文件@PropertySource的使用

@PropertySource:加载指定的配置文件; 因为当我们把所有配置文件都放在全局的配置文件 中时会导致配置文件过多,所以我们可以根据业务逻辑把配置文件分开来放

本次做了一个测试,当自定义配置文件后缀为yml时会导致注入失败,下面请看测试结果

1.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 maps;
     private List lists;

     private Dog dog;

     //get set
}

测试结果

Persion{name='null', age=null, isBoss=false, birth=null, maps=null, lists=null, dog=null}

你可能感兴趣的:(springboot,系列教学,springboot2.0)