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

Springboot @Value注入boolean設(shè)置默認(rèn)值方式

 更新時間:2022年03月18日 15:14:17   作者:碼狐  
這篇文章主要介紹了Springboot @Value注入boolean設(shè)置默認(rèn)值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@Value注入boolean設(shè)置默認(rèn)值

問題描述

Springboot 中讀取配置文件

test:

業(yè)務(wù)代碼如下

@Value("${test:true}")
private boolean test;

報錯如下

nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value []

問題分析

根據(jù)報錯可知,主要問題在于 注入時 test 的值是 String 類型,無法轉(zhuǎn)換成 boolean 類型。

@Value("${test:true}")
private String test;

于是更改了接收類型,看看獲取到的值是否是 true,結(jié)果發(fā)現(xiàn) test 值為 “”,而不是設(shè)置的默認(rèn)值

解決方案

報錯問題在于只要配置文件中有 test: 所以系統(tǒng)就默認(rèn) test 為 “” 而不是按照我所設(shè)想的為空所以默認(rèn)值為 true。

直接刪除配置文件中的 test: 即可正常啟動。

@Value 源碼閱讀

在排查問題的過程中也粗略的跟讀了一下源碼

//org.springframework.beans.TypeConverterSupport#doConvert()
private <T> T doConvert(Object value, Class<T> requiredType, MethodParameter methodParam, Field field) throws TypeMismatchException {
? ? ?try {
? ? ? ? ?return field != null ? this.typeConverterDelegate.convertIfNecessary(value, requiredType, field) : this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam);
? ? ?} catch (ConverterNotFoundException var6) {
? ? ? ? ?throw new ConversionNotSupportedException(value, requiredType, var6);
? ? ?} catch (ConversionException var7) {
? ? ? ? ?throw new TypeMismatchException(value, requiredType, var7);
? ? ?} catch (IllegalStateException var8) {
? ? ? ? ?throw new ConversionNotSupportedException(value, requiredType, var8);
? ? ?} catch (IllegalArgumentException var9) {
? ? ?// 最終異常從這里拋出
? ? ? ? ?throw new TypeMismatchException(value, requiredType, var9);
? ? ?}
?}

最終賦值在

//org.springframework.beans.TypeConverterDelegate#doConvertTextValue()
private Object doConvertTextValue(Object oldValue, String newTextValue, PropertyEditor editor) {
? ? try {
? ? ? ? editor.setValue(oldValue);
? ? } catch (Exception var5) {
? ? ? ? if (logger.isDebugEnabled()) {
? ? ? ? ? ? logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", var5);
? ? ? ? }
? ? }
?? ?// 此處發(fā)現(xiàn) newTextValue 為 ""
? ? editor.setAsText(newTextValue);
? ? return editor.getValue();
}

接下來就是如何將 字符串 true 轉(zhuǎn)換為 boolean 的具體代碼:

// org.springframework.beans.propertyeditors.CustomBooleanEditor#setAsText()
? ? public void setAsText(String text) throws IllegalArgumentException {
? ? ? ? String input = text != null ? text.trim() : null;
? ? ? ? if (this.allowEmpty && !StringUtils.hasLength(input)) {
? ? ? ? ? ? this.setValue((Object)null);
? ? ? ? } else if (this.trueString != null && this.trueString.equalsIgnoreCase(input)) {
? ? ? ? ? ? this.setValue(Boolean.TRUE);
? ? ? ? } else if (this.falseString != null && this.falseString.equalsIgnoreCase(input)) {
? ? ? ? ? ? this.setValue(Boolean.FALSE);
? ? ? ? } else if (this.trueString != null || !"true".equalsIgnoreCase(input) && !"on".equalsIgnoreCase(input) && !"yes".equalsIgnoreCase(input) && !"1".equals(input)) {
? ? ? ? ? ? if (this.falseString != null || !"false".equalsIgnoreCase(input) && !"off".equalsIgnoreCase(input) && !"no".equalsIgnoreCase(input) && !"0".equals(input)) {
? ? ? ? ? ? ? ? throw new IllegalArgumentException("Invalid boolean value [" + text + "]");
? ? ? ? ? ? }
? ? ? ? ? ? this.setValue(Boolean.FALSE);
? ? ? ? } else {
? ? ? ? ? ? this.setValue(Boolean.TRUE);
? ? ? ? }
? ? }

tips:windows 中使用 IDEA 去查找類可以使用 ctrl + shift +alt +N的快捷鍵組合去查詢,mac 系統(tǒng)則是 commond + O

Spring解析@Value

1、初始化PropertyPlaceholderHelper對象

? ? protected String placeholderPrefix = "${";
?
?? ?protected String placeholderSuffix = "}";
?? ?@Nullable
?? ?protected String valueSeparator = ":";?
private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<>(4);
?
?? ?static {
?? ??? ?wellKnownSimplePrefixes.put("}", "{");
?? ??? ?wellKnownSimplePrefixes.put("]", "[");
?? ??? ?wellKnownSimplePrefixes.put(")", "(");
?? ?}
?
public PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix,
?? ??? ??? ?@Nullable String valueSeparator, boolean ignoreUnresolvablePlaceholders) {
?
?? ??? ?Assert.notNull(placeholderPrefix, "'placeholderPrefix' must not be null");
?? ??? ?Assert.notNull(placeholderSuffix, "'placeholderSuffix' must not be null");
? ? ? ? //默認(rèn)值${
?? ??? ?this.placeholderPrefix = placeholderPrefix;
? ? ? ? //默認(rèn)值}
?? ??? ?this.placeholderSuffix = placeholderSuffix;
?? ??? ?String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix);
? ? ? ? //當(dāng)前綴為空或跟定義的不匹配,取傳入的前綴
?? ??? ?if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) {
?? ??? ??? ?this.simplePrefix = simplePrefixForSuffix;
?? ??? ?}
?? ??? ?else {
?? ??? ??? ?this.simplePrefix = this.placeholderPrefix;
?? ??? ?}
? ? ? ? //默認(rèn)值:
?? ??? ?this.valueSeparator = valueSeparator;
?? ??? ?this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
?? ?}

2、解析@Value 

protected String parseStringValue(
?? ??? ??? ?String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
?
?? ??? ?StringBuilder result = new StringBuilder(value);
? ? ? ? //是否包含前綴,返回第一個前綴的開始index
?? ??? ?int startIndex = value.indexOf(this.placeholderPrefix);
?? ??? ?while (startIndex != -1) {
? ? ? ? ? ? //找到最后一個后綴的index
?? ??? ??? ?int endIndex = findPlaceholderEndIndex(result, startIndex);
?? ??? ??? ?if (endIndex != -1) {
? ? ? ? ? ? ? ? //去掉前綴后綴,取出里面的字符串
?? ??? ??? ??? ?String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
?? ??? ??? ??? ?String originalPlaceholder = placeholder;
?? ??? ??? ??? ?if (!visitedPlaceholders.add(originalPlaceholder)) {
?? ??? ??? ??? ??? ?throw new IllegalArgumentException(
?? ??? ??? ??? ??? ??? ??? ?"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?// 遞歸判斷是否存在占位符,可以這樣寫${acm.endpoint:${address.server.domain:}}
?? ??? ??? ??? ?placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
?? ??? ??? ??? ?// 根據(jù)key獲取對應(yīng)的值
?? ??? ??? ??? ?String propVal = placeholderResolver.resolvePlaceholder(placeholder);
? ? ? ? ? ? ? ? // 值不存在,但存在默認(rèn)值的分隔符
?? ??? ??? ??? ?if (propVal == null && this.valueSeparator != null) {
? ? ? ? ? ? ? ? ? ? // 獲取默認(rèn)值的索引
?? ??? ??? ??? ??? ?int separatorIndex = placeholder.indexOf(this.valueSeparator);
?? ??? ??? ??? ??? ?if (separatorIndex != -1) {
? ? ? ? ? ? ? ? ? ? ? ? // 切掉默認(rèn)值的字符串
?? ??? ??? ??? ??? ??? ?String actualPlaceholder = placeholder.substring(0, separatorIndex);
? ? ? ? ? ? ? ? ? ? ? ? // 切出默認(rèn)值
?? ??? ??? ??? ??? ??? ?String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
? ? ? ? ? ? ? ? ? ? ? ? // 根據(jù)新的key獲取對應(yīng)的值
?? ??? ??? ??? ??? ??? ?propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
? ? ? ? ? ? ? ? ? ? ? ? // 如果值不存在,則把默認(rèn)值賦值給當(dāng)前值
?? ??? ??? ??? ??? ??? ?if (propVal == null) {
?? ??? ??? ??? ??? ??? ??? ?propVal = defaultValue;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
? ? ? ? ? ? ? ? // 如果當(dāng)前值不為NULL
?? ??? ??? ??? ?if (propVal != null) {
?? ??? ??? ??? ??? ?// 遞歸獲取存在占位符的值信息
?? ??? ??? ??? ??? ?propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
? ? ? ? ? ? ? ? ? ? // 替換占位符
?? ??? ??? ??? ??? ?result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
?? ??? ??? ??? ??? ?if (logger.isTraceEnabled()) {
?? ??? ??? ??? ??? ??? ?logger.trace("Resolved placeholder '" + placeholder + "'");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else if (this.ignoreUnresolvablePlaceholders) {
?? ??? ??? ??? ??? ?// Proceed with unprocessed value.
?? ??? ??? ??? ??? ?startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ?throw new IllegalArgumentException("Could not resolve placeholder '" +
?? ??? ??? ??? ??? ??? ??? ?placeholder + "'" + " in value \"" + value + "\"");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?visitedPlaceholders.remove(originalPlaceholder);
?? ??? ??? ?}
?? ??? ??? ?else {
?? ??? ??? ??? ?startIndex = -1;
?? ??? ??? ?}
?? ??? ?}?
?? ??? ?return result.toString();
?? ?}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳談java中int和Integer的區(qū)別及自動裝箱和自動拆箱

    詳談java中int和Integer的區(qū)別及自動裝箱和自動拆箱

    這篇文章主要介紹了詳談java中int和Integer的區(qū)別及自動裝箱和自動拆箱,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java反射機(jī)制詳解

    Java反射機(jī)制詳解

    Java的反射機(jī)制是在運(yùn)行狀態(tài)中,對于任何一個類,都可以知道這個類的所有屬性和方法,對于任何一個對象,都可以調(diào)用它所有的方法和屬性,修改部分類型信息,這種動態(tài)獲取信息以及動態(tài)調(diào)用對象方法的功能稱為Java的反射機(jī)制
    2022-09-09
  • Java spring單點(diǎn)登錄系統(tǒng)

    Java spring單點(diǎn)登錄系統(tǒng)

    這篇文章主要介紹了Java spring單點(diǎn)登錄系統(tǒng),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot項目里集成Hibernate的示例

    SpringBoot項目里集成Hibernate的示例

    在Spring Boot項目中,集成Hibernate可以幫助我們更輕松地進(jìn)行數(shù)據(jù)庫操作,本文將介紹如何在Spring Boot項目中集成Hibernate,并提供相應(yīng)的示例,感興趣的朋友跟隨小編一起看看吧
    2023-04-04
  • java中map和對象互轉(zhuǎn)工具類的實現(xiàn)示例

    java中map和對象互轉(zhuǎn)工具類的實現(xiàn)示例

    這篇文章主要介紹了java中map和對象互轉(zhuǎn)工具類的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • idea中的jvm調(diào)優(yōu)方式

    idea中的jvm調(diào)優(yōu)方式

    這篇文章主要介紹了idea中的jvm調(diào)優(yōu)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java二叉樹的四種遍歷(遞歸與非遞歸)

    Java二叉樹的四種遍歷(遞歸與非遞歸)

    這篇文章小編給大家分享的是Java二叉樹的四種遍歷,主要是遞歸與非遞歸,下面文章加u來詳細(xì)介紹,感興趣的小伙伴一起來學(xué)習(xí)吧
    2021-10-10
  • java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容

    java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容

    這篇文章主要介紹了java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot中加密模塊的使用

    SpringBoot中加密模塊的使用

    本文主要介紹了SpringBoot中加密模塊的使用,包括對稱加密、非對稱加密和哈希加密等,同時還會提供相應(yīng)的代碼示例,感興趣的朋友可以參考一下
    2023-05-05
  • SpringBoot使用@Async注解可能會遇到的8大坑點(diǎn)匯總

    SpringBoot使用@Async注解可能會遇到的8大坑點(diǎn)匯總

    SpringBoot中,@Async注解可以實現(xiàn)異步線程調(diào)用,用法簡單,體驗舒適,但是你一定碰到過異步調(diào)用不生效的情況,今天,我就列出90%的人都可能會遇到的8大坑點(diǎn),需要的朋友可以參考下
    2023-09-09

最新評論