gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:46:51 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com