【发布时间】:2013-03-01 22:06:38
【问题描述】:
我正在尝试将值的输出转换为整数:
@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;
上面抛出错误
org.springframework.beans.TypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer';
nested exception is java.lang.NumberFormatException:
For input string: "(java.lang.Integer)${api.orders.pingFrequency}"
我也试过@Value("(java.lang.Integer)${api.orders.pingFrequency}")
Google 似乎没有在这个主题上发表太多意见。我希望始终处理一个整数,而不是在使用它的任何地方解析这个值。
解决方法
我意识到一种解决方法可能是使用 setter 方法为我运行转换,但如果 Spring 可以做到,我宁愿学习一些有关 Spring 的知识。
【问题讨论】:
-
第一次尝试还是第二次例外?
-
@SotiriosDelimanolis 第一个
-
如果您遇到过这样的问题,最明显的原因是您忘记定义正确的 PropertySource。你需要: @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }
-
也可能是属性文件编码不当造成的(java需要ISO 8859-1)
-
属性占位符末尾缺少花括号对我造成了此错误:/
标签: java spring casting annotations property-placeholder