SpringBoot常用計量與bean屬性校驗和進制數(shù)據(jù)轉(zhuǎn)換規(guī)則全面分析
常用計量單位
SpringBoot支持JDK8提供的時間與空間計量單位
@Data //lombok省去get/set/toString @ConfigurationProperties(prefix = "servers") //配置文件的位置,對應(yīng)的屬性注入 public class ServerConfig { private String ipAddress; private int port; private String detail; //時間 private Duration serverTimeOut; //存儲空間 private DataSize dataSize; }
配置文件中可以在之后直接加單位
servers:
ipAddress: 192.158.0.1
port: 1234
detail: zhangsan
serverTimeOut: 10s
dataSize: 4MB
運行結(jié)果
或者不在配置文件加單位,在實體類中加注解
bean屬性校驗
開啟Bean數(shù)據(jù)校驗
①添加JSR303規(guī)范坐標與Hibernate校驗框架對應(yīng)坐標
<!-- 提供規(guī)范的接口--> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </dependency> <!-- 對上面接口的實現(xiàn)--> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> </dependency>
②:@Validated開啟校驗功能
③:設(shè)置校驗規(guī)則
@Data //lombok省去get/set/toString //@Component //交給spring容器管理 @ConfigurationProperties(prefix = "servers") //配置文件的位置,對應(yīng)的屬性注入 //②開啟校驗 @Validated public class ServerConfig { private String ipAddress; // ③設(shè)置校驗規(guī)則 @Max(value=9000,message = "最大不能超過9000") @Min(value=100,message = "最大不能低于100") private int port; private String detail; private Duration serverTimeOut; private DataSize dataSize; }
運行之后,當(dāng)設(shè)置的校驗不在這個范圍則會報錯
小結(jié):
啟用Bean屬性校驗
導(dǎo)入JSR303與Hibernate校驗框架坐標
使用@Validated注解啟用校驗功能
使用具體校驗規(guī)則規(guī)范數(shù)據(jù)校驗格式
進制數(shù)據(jù)轉(zhuǎn)換規(guī)則
配置文件中的數(shù)字存在進制轉(zhuǎn)換
當(dāng)以0開頭,且后續(xù)為(0-7),會自動轉(zhuǎn)為八進制
當(dāng)以0x開頭,且后續(xù)為(0-9,a-f)會自動轉(zhuǎn)為十六進制
application.yml下
mysql:
Password1: 01234
Password2: 0x2345
Password3: 0187
Password4: "01234"
test類
package com; import com.pojo.ServerConfig; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @SpringBootTest class Springboot11ConfigurationApplicationTests { @Value("${mysql.Password1}") private String password1; @Value("${mysql.Password2}") private String password2; @Value("${mysql.Password3}") private String password3; @Value("${mysql.Password4}") private String password4; @Test void contextLoads() { System.out.println(password1); System.out.println(password2); System.out.println(password3); System.out.println(password4); } }
運行結(jié)果:
可以看出當(dāng)加上雙引號不會發(fā)生進制轉(zhuǎn)換,所以在進行這類操作時,應(yīng)當(dāng)加上雙引號。當(dāng)十進制以0開頭,會到之后
yaml字面值表達式方式
到此這篇關(guān)于SpringBoot常用計量與bean屬性校驗和進制數(shù)據(jù)轉(zhuǎn)換規(guī)則全面分析的文章就介紹到這了,更多相關(guān)SpringBoot常用計量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)excel文件生成和下載
這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)excel文件生成和下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02JavaWeb 網(wǎng)上書店 注冊和登陸功能案例詳解
這篇文章主要介紹了JavaWeb 網(wǎng)上書店 注冊和登陸功能,結(jié)合具體案例形式詳細分析了JavaWeb 網(wǎng)上書店 注冊和登陸功能具體實現(xiàn)步驟、操作技巧與注意事項,需要的朋友可以參考下2019-08-08Spring Cloud Feign請求添加headers的實現(xiàn)方式
這篇文章主要介紹了Spring Cloud Feign請求添加headers的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04