java - Spring @Value TypeMismatchException :Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'

标签 java spring spring-el spring-properties property-placeholder

我想用@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/

相关文章:

java - 在Java中获取系统环境['HOSTNAME']

java - 使用Spring表达式语言编译Pattern对象

java - 在Spring中如何在注入(inject)期间连接静态方法的结果

java - GridBagLayOut 向上跨越列?

java - 使用 hibernate session /JDO persistenceManager 时的 ThreadLocal

spring - TomEE 数据源资源获取 "NoWait: Pool empty. Unable to fetch a connection, none available[5 in use]."

mysql - hibernate 通信链接失败

java - 如何对 java.util.Arrays.toString 使用静态导入?

java - Spring 错误: No property prod found for type Product

java - 在 Spring Boot 1.4 中测试安全性