欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot數(shù)據(jù)校驗(yàn)及多環(huán)境配置的問題詳解

 更新時(shí)間:2021年09月22日 10:40:02   作者:風(fēng)棲祈鳶  
這篇文章主要介紹了SpringBoot數(shù)據(jù)校驗(yàn)及多環(huán)境配置,本文以SpringBoot-02-Config 項(xiàng)目為例,給大家詳細(xì)介紹,需要的朋友可以參考下

接上節(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 的依賴

 <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ù)校驗(yàn),在 name 屬性上添加 @Email 注解,表明這個(gè)屬性要符合 Email 的格式

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component //注冊為 bean
@ConfigurationProperties(prefix = "person")
// 開啟數(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)行測試方法,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)可以方便地對屬性的值進(jìn)行合法性檢測,在 JSR303 規(guī)范中( Java Specification Requests,即 Java 規(guī)范提案,JSR-303 是 JAVA EE 6 中的一項(xiàng)子規(guī)范)還有許多這樣的檢測注釋,用到的時(shí)候再查吧!

2. 多環(huán)境配置

在 Spring 中可以使用 profile 對不同的環(huán)境進(jìn)行不同的配置設(shè)置,通過激活不同的環(huán)境版本,實(shí)現(xiàn)快速切換環(huán)境。

在編寫配置文件的時(shí)候,文件名可以是 application-{profile}.properties/yml,通過不同的 profile 指定不同的環(huán)境,如 application-test.properties 表示測試環(huán)境,application-dev.properties 表示開發(fā)環(huán)境;但 SpringBoot 不會(huì)直接使用這種配置文件,它默認(rèn)使用的是 application.properties 配置文件,所以需要指定需要使用的環(huán)境

spring.profiles.active=dev

若使用 yaml 進(jìn)行配置,則更加簡單了;yaml 提供了多文檔塊功能,不用創(chuàng)建多個(gè)配置文件

server:
  port: 8081
#選擇要激活那個(gè)環(huán)境塊
spring:
  profiles:
    active: test
    
---
server:
  port: 8082
spring:
  profiles: dev #配置環(huán)境的名稱

---
server:
  port: 8083
spring:
  profiles: prod #配置環(huán)境的名稱

注意:如果 properties 和 yaml 都進(jìn)行了端口配置,且沒有指定其他配置,會(huì)默認(rèn)使用 properties 配置文件。

3. 配置文件加載位置

SpringBoot 會(huì)掃描以下位置的 application.propertiesapplication.yml 文件作為默認(rèn)配置文件,優(yōu)先級順序?yàn)?/p>

  • 項(xiàng)目路徑下的 config 文件夾中的配置文件:file:./config/
  • 項(xiàng)目路徑下的配置文件:file:./
  • 資源路徑下的 config 文件夾中的配置文件:classpath:./config/
  • 資源路徑下的配置文件:classpath:./

優(yōu)先級由高到底,高優(yōu)先級的配置會(huì)覆蓋低優(yōu)先級的配置;若沒有沖突,則配置會(huì)互補(bǔ)!

4. 總結(jié)

到此這篇關(guān)于SpringBoot數(shù)據(jù)校驗(yàn)及多環(huán)境配置的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)校驗(yàn)多環(huán)境配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論