使用的configurationProperties含義
application.yml 和 application.properties等方便了我們進(jìn)行配置的管理和統(tǒng)一,但是在使用的是時(shí)候,我們還是通過@Value("${xxxx}")的形式去使用,例如
@Value("${test.name}")
private String name;
@Value("${test.age}")
private int age;
@Value("${test.address}")
private String address;
但是使用了@ConfigurationProperties配置之后,可以進(jìn)一步簡化@Value()的使用,最后的結(jié)果會(huì)變成這個(gè)簡單
@Data
@Component
@ConfigurationProperties(prefix = "test")
public class TestConfigurationPropertyBean {
// @Value("${test.name}")
private String name;
// @Value("${test.age}")
private int age;
// @Value("${test.address}")
private String address;
}
可以不需要在pom.xml中增加依賴就可以使用了,反正我是沒有加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
開始使用
1.在yml文件中寫好配置參數(shù)
test:
name: lirihong
addres: nanshan
age: 18
- 聲明一個(gè)對象
@Data
@Component
//@EnableConfigurationProperties(TestConfigurationPropertyBean.class)
@ConfigurationProperties(prefix = "test")
public class TestConfigurationPropertyBean {
// @Value("${test.name}")
private String name;
// @Value("${test.age}")
private int age;
// @Value("${test.address}")
private String address;
}
添加@ConfigurationProperties注解在類的頭部,并加上prefix的前綴,指定查找對應(yīng)前綴test的配置
bean對象要有g(shù)et 和set 的方法,所以使用了@Data注解
最后使用@Component 將對象都暴露成一個(gè)組件,交給spring去管理,方便其他得放的使用,不推薦使用@EnableConfigurationProperties(TestConfigurationPropertyBean.class)注解
3.編寫測試類來進(jìn)行測試
@SpringBootTest
class StudyApplicationTests {
@Test
void contextLoads() {
}
@Autowired
TestConfigurationPropertyBean testConfigurationPropertyBean;
@Test
public void testConfigurationProperties() {
System.out.println(testConfigurationPropertyBean.getAddress());
System.out.println(testConfigurationPropertyBean.getAge());
System.out.println(testConfigurationPropertyBean.getName());
//System.out.println(testConfigurationPropertyBean.getChinese());
//System.out.println(testConfigurationPropertyBean.getMath());
//System.out.println(testConfigurationPropertyBean.getEnglish());
}
}
-
輸出結(jié)果
image.png
基本完成
進(jìn)階
- 混合使用@ConfigurationProperties 和@Value
有時(shí)候如果一個(gè)bean對象能盡量的內(nèi)聚成只有和自己相關(guān)的參數(shù)屬性,那是最好的,這樣只需要使用一個(gè)@ConfigurationProperties就可以完成配置,但是總有一些情況,比如如果是對象是Controller,牽涉的和對象比較多,這個(gè)時(shí)候如果使用一個(gè)@ConfigurationProperties可能無法完成所有的配置注入,這個(gè)時(shí)候,還是可以使用原有的@Value配置進(jìn)行注入值,從而達(dá)到效果
application.yml
test:
name: lirihong
address: nanshan
age: 18
score:
Chinese: 98
math: 100
English: 99
bean
@Data
@Component
//@EnableConfigurationProperties(TestConfigurationPropertyBean.class)
@ConfigurationProperties(prefix = "test")
//@ConfigurationProperties(prefix = "score")
public class TestConfigurationPropertyBean {
// @Value("${test.name}")
private String name;
// @Value("${test.age}")
private int age;
// @Value("${test.address}")
private String address;
@Value("${score.Chinese}")
private int Chinese;
@Value("${score.math}")
private int math;
@Value("${score.English}")
private int English;
}
輸出結(jié)果

-
加上參數(shù)校驗(yàn)@Validated
在類的頭部加上@Validated 注解,在字段上加上@NotEmpty等注解,在注入值的時(shí)候會(huì)自動(dòng)校驗(yàn)值的參數(shù),不符合則報(bào)錯(cuò)
例如 application.yml
image.png
bean
image.png
啟動(dòng)服務(wù) 就會(huì)報(bào)錯(cuò),錯(cuò)誤信息中會(huì)提示我們是name 子彈注入了空值,但是他是不允許為空的

3.未知的屬性 ignoreUnknownFields
使用改注解可以盡量的查找出在yml中聲明了,但是沒有用的注解,這樣可以簡化和刪除一些當(dāng)初設(shè)定了但是后面沒有用到的注解。默認(rèn)ignoreUnknownFields = true,即使有多余的配置,也會(huì)忽略不管,但是將參數(shù)設(shè)置為false之后ignoreUnknownFields = false,服務(wù)啟動(dòng)的時(shí)候就會(huì)全部查找所有的yml 或者properties配置進(jìn)行匹配,發(fā)現(xiàn)存在沒有匹配的,都會(huì)報(bào)錯(cuò)
yml

bean

運(yùn)行報(bào)錯(cuò)

-
慎用參數(shù) ignoreInvalidFields
如果我們在 application.yml屬性上定義的屬性不能被正確的解析會(huì)發(fā)生什么?假如我們?yōu)樵緫?yīng)該為布爾值的屬性提供的值為 'pass':
image.png
但是需要的是一個(gè)boolean 的值
image.png
默認(rèn)情況下,Spring Boot 將會(huì)啟動(dòng)失敗,并拋出異常:

如果將ignoreInvalidFields = true,那么程序啟動(dòng)的時(shí)候?qū)?huì)忽略這種轉(zhuǎn)換錯(cuò)誤的值,但是這樣會(huì)給后續(xù)的程序排錯(cuò)帶來更多不必要的麻煩,所以還是使用默認(rèn)值false,程序啟動(dòng)的時(shí)候就會(huì)自動(dòng)檢驗(yàn)轉(zhuǎn)換是否符合要求。




