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

前端 未结 3 1869
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 15:45

I want to use the @Value annotation to inject a Double property such as:

@Service
public class MyService {

    @Value(\"${item.priceFactor}\")
    private D         


        
相关标签:
3条回答
  • 2020-12-14 15:53

    Try changing the following line

    @Value("${item.priceFactor}")
    

    to

    @Value("#{new Double('${item.priceFactor}')}")
    
    0 讨论(0)
  • 2020-12-14 16:05

    How about storing a String and converting to numbers like integers and doubles through getters and setters? For safe code with Java injection you should always use getters and setters and only for other methods in any case. I advice you warmly to read about java security (Which is NOT for hackers), but more for code usage and writing likewise the one you uploaded, wich uses injection.

    0 讨论(0)
  • 2020-12-14 16:09

    This should solve the problem-

    @Value("#{T(Double).parseDouble('${item.priceFactor}')}")
    private Double priceFactor;
    
    0 讨论(0)
提交回复
热议问题