接上節(jié),本節(jié)補充一下數(shù)據(jù)校驗及多環(huán)境配置的內容,仍是 SpringBoot-02-Config 項目。
1. 數(shù)據(jù)校驗
使用數(shù)據(jù)校驗,可以在輸入不合法數(shù)據(jù)時拋出異常,首先要添加 validation
的依賴
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 類上使用 @Validated
注解開啟數(shù)據(jù)校驗,在 name 屬性上添加 @Email
注解,表明這個屬性要符合 Email 的格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Data @AllArgsConstructor @NoArgsConstructor @Component //注冊為 bean @ConfigurationProperties (prefix = "person" ) // 開啟數(shù)據(jù)校驗 @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,是不合法的,這時運行測試方法,SpringBoot 會報錯
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: 不是一個合法的電子郵件地址
查看底層的錯誤,也可以看到
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 [不是一個合法的電子郵件地址]; origin class path resource [application.yaml] - 2:9
總而言之,使用數(shù)據(jù)校驗可以方便地對屬性的值進行合法性檢測,在 JSR303 規(guī)范中( Java Specification Requests,即 Java 規(guī)范提案,JSR-303 是 JAVA EE 6 中的一項子規(guī)范)還有許多這樣的檢測注釋,用到的時候再查吧!
2. 多環(huán)境配置
在 Spring 中可以使用 profile 對不同的環(huán)境進行不同的配置設置,通過激活不同的環(huán)境版本,實現(xiàn)快速切換環(huán)境。
在編寫配置文件的時候,文件名可以是 application-{profile}.properties/yml
,通過不同的 profile 指定不同的環(huán)境,如 application-test.properties
表示測試環(huán)境,application-dev.properties
表示開發(fā)環(huán)境;但 SpringBoot 不會直接使用這種配置文件,它默認使用的是 application.properties
配置文件,所以需要指定需要使用的環(huán)境
1
|
spring.profiles.active=dev |
若使用 yaml 進行配置,則更加簡單了;yaml 提供了多文檔塊功能,不用創(chuàng)建多個配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
server: port: 8081 #選擇要激活那個環(huán)境塊 spring: profiles: active: test --- server: port: 8082 spring: profiles: dev #配置環(huán)境的名稱 --- server: port: 8083 spring: profiles: prod #配置環(huán)境的名稱 |
注意:如果 properties 和 yaml 都進行了端口配置,且沒有指定其他配置,會默認使用 properties 配置文件。
3. 配置文件加載位置
SpringBoot 會掃描以下位置的 application.properties
或 application.yml
文件作為默認配置文件,優(yōu)先級順序為
-
項目路徑下的 config 文件夾中的配置文件:
file:./config/
-
項目路徑下的配置文件:
file:./
-
資源路徑下的 config 文件夾中的配置文件:
classpath:./config/
-
資源路徑下的配置文件:
classpath:./
優(yōu)先級由高到底,高優(yōu)先級的配置會覆蓋低優(yōu)先級的配置;若沒有沖突,則配置會互補!
4. 總結
到此這篇關于SpringBoot數(shù)據(jù)校驗及多環(huán)境配置的文章就介紹到這了,更多相關SpringBoot數(shù)據(jù)校驗多環(huán)境配置內容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_43560701/article/details/120379942