最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

問(wèn)題描述

Springboot 中讀取配置文件

test:

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

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

報(bào)錯(cuò)如下

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 []

問(wèn)題分析

根據(jù)報(bào)錯(cuò)可知,主要問(wèn)題在于 注入時(shí) test 的值是 String 類(lèi)型,無(wú)法轉(zhuǎn)換成 boolean 類(lèi)型。

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

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

解決方案

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

直接刪除配置文件中的 test: 即可正常啟動(dòng)。

@Value 源碼閱讀

在排查問(wèn)題的過(guò)程中也粗略的跟讀了一下源碼

//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();
}

接下來(lái)就是如何將 字符串 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 去查找類(lèi)可以使用 ctrl + shift +alt +N的快捷鍵組合去查詢,mac 系統(tǒng)則是 commond + O

Spring解析@Value

1、初始化PropertyPlaceholderHelper對(duì)象

? ? 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);
? ? ? ? //是否包含前綴,返回第一個(gè)前綴的開(kāi)始index
?? ??? ?int startIndex = value.indexOf(this.placeholderPrefix);
?? ??? ?while (startIndex != -1) {
? ? ? ? ? ? //找到最后一個(gè)后綴的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");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?// 遞歸判斷是否存在占位符,可以這樣寫(xiě)${acm.endpoint:${address.server.domain:}}
?? ??? ??? ??? ?placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
?? ??? ??? ??? ?// 根據(jù)key獲取對(duì)應(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獲取對(duì)應(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();
?? ?}

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

相關(guān)文章

  • Java中與數(shù)字相關(guān)的常用類(lèi)的用法詳解

    Java中與數(shù)字相關(guān)的常用類(lèi)的用法詳解

    在我們的代碼中,經(jīng)常會(huì)遇到一些數(shù)字&數(shù)學(xué)問(wèn)題、隨機(jī)數(shù)問(wèn)題、日期問(wèn)題和系統(tǒng)設(shè)置問(wèn)題等,為了解決這些問(wèn)題,Java給我們提供了多個(gè)處理相關(guān)問(wèn)題的類(lèi),比如Number類(lèi)、Math類(lèi)、Random類(lèi)等等,本篇文章我們先從Number數(shù)字類(lèi)和Math數(shù)學(xué)類(lèi)學(xué)起
    2023-05-05
  • Java中List常用操作比f(wàn)or循環(huán)更優(yōu)雅的寫(xiě)法示例

    Java中List常用操作比f(wàn)or循環(huán)更優(yōu)雅的寫(xiě)法示例

    List是Java中比較常用的集合類(lèi),關(guān)于List接口有很多實(shí)現(xiàn)類(lèi),下面這篇文章主要給大家介紹了關(guān)于Java中List常用操作比f(wàn)or循環(huán)更優(yōu)雅的寫(xiě)法,需要的朋友可以參考下
    2021-11-11
  • java正則表達(dá)式學(xué)習(xí)筆記之命名捕獲

    java正則表達(dá)式學(xué)習(xí)筆記之命名捕獲

    這篇文章主要為大家詳細(xì)介紹了java正則表達(dá)式中的命名捕獲,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Mybatis Plus使用@TableId的示例詳解

    Mybatis Plus使用@TableId的示例詳解

    在 MyBatis Plus 中,@TableId 注解是用于標(biāo)記實(shí)體類(lèi)中的主鍵字段,它可以更方便地處理主鍵相關(guān)的操作,如自動(dòng)填充主鍵值或識(shí)別主鍵字段,這篇文章主要介紹了Mybatis Plus使用@TableId,需要的朋友可以參考下
    2024-08-08
  • java使用jdbc連接數(shù)據(jù)庫(kù)工具類(lèi)和jdbc連接mysql數(shù)據(jù)示例

    java使用jdbc連接數(shù)據(jù)庫(kù)工具類(lèi)和jdbc連接mysql數(shù)據(jù)示例

    這篇文章主要介紹了java使用jdbc連接數(shù)據(jù)庫(kù)的工具類(lèi)和使用jdbc連接mysql數(shù)據(jù)的示例,需要的朋友可以參考下
    2014-03-03
  • RocketMQ源碼解析topic創(chuàng)建機(jī)制詳解

    RocketMQ源碼解析topic創(chuàng)建機(jī)制詳解

    這篇文章主要為大家介紹了RocketMQ源碼解析topic創(chuàng)建機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 詳解Java中Object?類(lèi)的使用

    詳解Java中Object?類(lèi)的使用

    Java的Object?類(lèi)是所有類(lèi)的父類(lèi),也就是說(shuō)?Java?的所有類(lèi)都繼承了?Object,本文主要來(lái)和大家講講Object?類(lèi)的使用,感興趣的可以了解一下
    2023-05-05
  • SpringBoot+VUE實(shí)現(xiàn)前后端分離的實(shí)戰(zhàn)記錄

    SpringBoot+VUE實(shí)現(xiàn)前后端分離的實(shí)戰(zhàn)記錄

    這篇文章主要介紹了SpringBoot+VUE實(shí)現(xiàn)前后端分離的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • sharding-jdbc中的事務(wù)詳細(xì)解讀

    sharding-jdbc中的事務(wù)詳細(xì)解讀

    這篇文章主要介紹了sharding-jdbc中的事務(wù)詳細(xì)解讀,sharding-jdbc在分庫(kù)分表方面提供了很大的便利性,在使用DB的時(shí)候,通常都會(huì)涉及到事務(wù)這個(gè)概念,而在分庫(kù)分表的環(huán)境上再加上事務(wù),就會(huì)使事情變得復(fù)雜起來(lái),需要的朋友可以參考下
    2023-12-12
  • Java中this關(guān)鍵字的用法詳解

    Java中this關(guān)鍵字的用法詳解

    我知道很多朋友都和我一樣,在JAVA程序中似乎經(jīng)常見(jiàn)到this,自己也偶爾用到它,但是到底this該怎么用,卻心中無(wú)數(shù),下面這篇文章主要給大家介紹了關(guān)于Java中this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評(píng)論

德江县| 汽车| 精河县| 瑞丽市| 镇安县| 水城县| 乡城县| 乳山市| 沁水县| 蓬安县| 阜宁县| 河西区| 阳西县| 抚松县| 清镇市| 涞源县| 屏山县| 西吉县| 和硕县| 嘉定区| 米林县| 磐石市| 洪江市| 尼勒克县| 望城县| 任丘市| 岳阳市| 泽库县| 乌拉特中旗| 依兰县| 阳信县| 临夏市| 太仆寺旗| 汝南县| 仁怀市| 晴隆县| 江门市| 连山| 道真| 秦安县| 丘北县|