如果属性是 Integer,Spring @Value 总是给出错误

2023-11-22

我正在使用 sprin 版本 4.3.8.RELEASE。我也在用@Value从属性文件注入值,如果属性是字符串,没有问题,但如果属性是Integer这是一个问题(我知道有很多关于此的问题,我尝试了所有答案,但问题仍然存在)

该物业是

CONNECTION.TIME.OUT=100000

第一个解决方案

@Value("${CONNECTION.TIME.OUT}")
protected Integer connectionTimeOut;

例外

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:10}")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果属性是 Integer,Spring @Value 总是给出错误 的相关文章

随机推荐