如果属性为Integer,则Spring @Value始终会给出错误 [英] Spring @Value always gives an error if property is Integer
问题描述
我正在使用sprin版本4.3.8.RELEASE.我也正在使用 @Value
从属性文件注入值,如果属性是字符串,那没问题,但是如果属性是 Integer
,那是一个问题(我知道有很多问题,我尝试了所有答案,但问题仍然存在)
I am using sprin version 4.3.8.RELEASE. also i am using @Value
to inject values from property file, if the properties are string that no problem, but if the property is Integer
that is a problem (i know there is many questions about this i tried all the answers but the issue still exist)
该属性是
CONNECTION.TIME.OUT=100000
第一个解决方案
@Value("${CONNECTION.TIME.OUT}")
protected Integer connectionTimeOut;
Ecxeption
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${CONNECTION.TIME.OUT}"
第二个解决方案
@Value("#{new Integer('${CONNECTION.TIME.OUT}')}")
protected Integer connectionTimeOut;
例外
EL1003E: A problem occurred whilst attempting to construct an object of type 'Integer' using arguments '(java.lang.String)'
第三种解决方案
@Value("#{new Integer.parseInteger('${CONNECTION.TIME.OUT}')}")
protected Integer connectionTimeOut;
例外
EL1003E: A problem occurred whilst attempting to construct an object of type 'Integer' using arguments '(java.lang.String)'
这是为什么的任何想法
推荐答案
对于 @Value("$ {CONNECTION.TIME.OUT}")
,您的错误是 java.lang.NumberFormatException:对于输入字符串:"$ {CONNECTION.TIME.OUT}"
.这意味着未处理表达式会导致 Integer.parseInt("$ {CONNECTION.TIME.OUT}")
引发 NumberFormatException
.
For @Value("${CONNECTION.TIME.OUT}")
your error is java.lang.NumberFormatException: For input string: "${CONNECTION.TIME.OUT}"
. This means that expression was not processed resulting in Integer.parseInt("${CONNECTION.TIME.OUT}")
which thrown the NumberFormatException
.
在Spring上下文中没有注册 PropertyPlaceholderConfigurer
bean,并且未处理 @Value
批注,或者没有属性 CONNECTION.TIME.OUT 代码>.
Either there is no PropertyPlaceholderConfigurer
bean registered in the Spring context and @Value
annotations are not processed or there is no property CONNECTION.TIME.OUT
defined.
这篇关于如果属性为Integer,则Spring @Value始终会给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!