當獲取主配置文件中屬性值時,只需@ConfigurationProperties(prefix = "person")注解來修飾某類,其作用是告訴springBoot,此類中的屬性將與默認的全局配置文件中對應屬性一一綁定。屬性名必須是application.yml或application.properties。【prefix = "person"】表示與配置文件中哪個層級的屬性進行綁定。
當一些屬性不想配置到主配置文件,需自定義一個配置文件,需通過@PropertySource注解指定此配置文件路徑。
而@ConfigurationProperties(prefix = "xxx")注解指定自定義配置文件中哪個層級屬性需綁定。
配置文件的位置:\src\main\resources\application.yml
一、bean類與主配置文件的綁定
案例:bean類Person.java
@Component
@ConfigurationProperties(prefix = "person")//只能從默認的全局文件中獲取
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//======================get 、set方法省略=============
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
配置文件:
person:
lastName: fang \n xin \n de
age: 18
boss: false
birth: 2018/12/10
maps: {a1: fang, a2: li,a3: zhang}
lists: [cat,pig,dog]
dog:
name: xiaogou10號
age: 1
測試案例:
package com.atguigu.springboot;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
打印結果:
Person{lastName='fang \n xin \n de', age=18, boss=false, birth=Mon Dec 10 00:00:00 CST 2018, maps={a1=fang, a2=li, a3=zhang}, lists=[cat, pig, dog], dog=Dog{name='xiaogou10號', age=1}}
二、bean類與自定義的配置文件綁定,需@ConfigurationProperties、@PropertySource兩個注解一起修飾bean類
@PropertySource指定自定義配置文件的地址
@ConfigurationProperties指定綁定屬性的層級
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value ={"classpath:person.properties"})
@Validated
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//==================set、get方法省略=======================
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
自定義配置文件person.properties:
person.lastName=李次方1110
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog01
person.dog.age=15
打印的結果:
Person{lastName='李次方1110', age=12, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='dog01', age=15}}
三、@ConfigurationProperties注解的特點
1.批量注入配置文件中值
2.支持松散綁定
配置文件中屬性格式分別為lastname 、last_name、last-name 時,都可以與Person類中屬性lastName綁定
person:
lastname: fang \n xin \n de
3.支持JSR303校驗:
Person類上加上@Validated,表示類中屬性需要進行驗證。 @Email表示lastName必須滿足郵件格式
@Component
@ConfigurationProperties(prefix = "person")//只能從默認的全局文件中獲取
@Validated
public class Person {
@Email
private String lastName;
}
配置文件中lastname值不是郵件格式
person:
lastname: fang \n xin \n de
輸出person對應的實例時報錯:
4.能夠封裝復雜結構的屬性:例如:map集合、級聯屬性
5.不支持SpEL表達式
參考:SpringBoot之@ConfigurationProperties、@PropertySource注解的使用