接上節(jié),本節(jié)補(bǔ)充一下數(shù)據(jù)校驗(yàn)及多環(huán)境配置的內(nèi)容,仍是 SpringBoot-02-Config 項(xiàng)目。
1. 數(shù)據(jù)校驗(yàn)
使用數(shù)據(jù)校驗(yàn),可以在輸入不合法數(shù)據(jù)時(shí)拋出異常,首先要添加 validation
的依賴(lài)
1
2
3
4
5
6
7
8
9
10
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version> 2.0 . 1 .Final</version> </dependency> |
在之前的 Person 類(lèi)上使用 @Validated
注解開(kāi)啟數(shù)據(jù)校驗(yàn),在 name 屬性上添加 @Email
注解,表明這個(gè)屬性要符合 Email 的格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Data @AllArgsConstructor @NoArgsConstructor @Component //注冊(cè)為 bean @ConfigurationProperties (prefix = "person" ) // 開(kāi)啟數(shù)據(jù)校驗(yàn) @Validated public class Person { // 檢查 name 符合郵箱格式 @Email () private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; } |
配置文件中注入的 name 屬性為 qiyuan,是不合法的,這時(shí)運(yùn)行測(cè)試方法,SpringBoot 會(huì)報(bào)錯(cuò)
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'person' to com.qiyuan.entity.Person failed:
Property: person.name
Value: qiyuan
Origin: class path resource [application.yaml] - 2:9
Reason: 不是一個(gè)合法的電子郵件地址
查看底層的錯(cuò)誤,也可以看到
Caused by: org.springframework.boot.context.properties.bind.validation.BindValidationException: Binding validation errors on person
- Field error in object 'person' on field 'name': rejected value [qiyuan]; codes [Email.person.name,Email.name,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.name,name]; arguments []; default message [name],[Ljavax.validation.constraints.Pattern$Flag;@44f3fe83,.*]; default message [不是一個(gè)合法的電子郵件地址]; origin class path resource [application.yaml] - 2:9
總而言之,使用數(shù)據(jù)校驗(yàn)可以方便地對(duì)屬性的值進(jìn)行合法性檢測(cè),在 JSR303 規(guī)范中( Java Specification Requests,即 Java 規(guī)范提案,JSR-303 是 JAVA EE 6 中的一項(xiàng)子規(guī)范)還有許多這樣的檢測(cè)注釋?zhuān)玫降臅r(shí)候再查吧!
2. 多環(huán)境配置
在 Spring 中可以使用 profile 對(duì)不同的環(huán)境進(jìn)行不同的配置設(shè)置,通過(guò)激活不同的環(huán)境版本,實(shí)現(xiàn)快速切換環(huán)境。
在編寫(xiě)配置文件的時(shí)候,文件名可以是 application-{profile}.properties/yml
,通過(guò)不同的 profile 指定不同的環(huán)境,如 application-test.properties
表示測(cè)試環(huán)境,application-dev.properties
表示開(kāi)發(fā)環(huán)境;但 SpringBoot 不會(huì)直接使用這種配置文件,它默認(rèn)使用的是 application.properties
配置文件,所以需要指定需要使用的環(huán)境
1
|
spring.profiles.active=dev |
若使用 yaml 進(jìn)行配置,則更加簡(jiǎn)單了;yaml 提供了多文檔塊功能,不用創(chuàng)建多個(gè)配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
server: port: 8081 #選擇要激活那個(gè)環(huán)境塊 spring: profiles: active: test --- server: port: 8082 spring: profiles: dev #配置環(huán)境的名稱(chēng) --- server: port: 8083 spring: profiles: prod #配置環(huán)境的名稱(chēng) |
注意:如果 properties 和 yaml 都進(jìn)行了端口配置,且沒(méi)有指定其他配置,會(huì)默認(rèn)使用 properties 配置文件。
3. 配置文件加載位置
SpringBoot 會(huì)掃描以下位置的 application.properties
或 application.yml
文件作為默認(rèn)配置文件,優(yōu)先級(jí)順序?yàn)?/p>
-
項(xiàng)目路徑下的 config 文件夾中的配置文件:
file:./config/
-
項(xiàng)目路徑下的配置文件:
file:./
-
資源路徑下的 config 文件夾中的配置文件:
classpath:./config/
-
資源路徑下的配置文件:
classpath:./
優(yōu)先級(jí)由高到底,高優(yōu)先級(jí)的配置會(huì)覆蓋低優(yōu)先級(jí)的配置;若沒(méi)有沖突,則配置會(huì)互補(bǔ)!
4. 總結(jié)
到此這篇關(guān)于SpringBoot數(shù)據(jù)校驗(yàn)及多環(huán)境配置的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)校驗(yàn)多環(huán)境配置內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_43560701/article/details/120379942