Spring @Value TypeMismatchException:无法将类型'java.lang.String'的值转换为必需类型'java.lang.Double' [英] Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'
问题描述
我想使用@Value注释注入Double属性,例如:
I want to use the @Value annotation to inject a Double property such as:
@Service
public class MyService {
@Value("${item.priceFactor}")
private Double priceFactor = 0.1;
// ...
并使用Spring属性占位符(属性文件) ):
and using Spring property placeholder (Properties files):
item.priceFactor=0.1
我得到例外:
org.springframework.beans.TypeMismatchException:无法转换
'java.lang.String'类型的值为必需类型'java.lang.Double';
嵌套异常是java.lang.NumberFormatException:对于输入字符串:
$ {item.priceFactor}
org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'; nested exception is java.lang.NumberFormatException: For input string: "${item.priceFactor}"
有没有办法使用来自属性文件的Double值?
Is there a way to use a Double value coming from a properties file?
推荐答案
尝试更改以下行
@Value("${item.priceFactor}")
到
@Value("#{new Double('${item.priceFactor}')}")
这篇关于Spring @Value TypeMismatchException:无法将类型'java.lang.String'的值转换为必需类型'java.lang.Double'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!