我想用@Value注解注入(inject)一个Double属性如:
@Service
public class MyService {
@Value("${item.priceFactor}")
private Double priceFactor = 0.1;
// ...
并使用 Spring 属性占位符(属性文件):
item.priceFactor=0.1
我得到异常:
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 值?
最佳答案
尝试更改以下行
@Value("${item.priceFactor}")
到
@Value("#{new Double('${item.priceFactor}')}")
关于java - Spring @Value TypeMismatchException :Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42733067/