Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.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;  // ... 

and using Spring property placeholder (Properties files):

item.priceFactor=0.1 

I get Exception:

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}"

Is there a way to use a Double value coming from a properties file?

like image 647
guilhebl Avatar asked Mar 11 '17 08:03

guilhebl


1 Answers

Try changing the following line

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

to

@Value("#{new Double('${item.priceFactor}')}") 
like image 149
bcr666 Avatar answered Sep 23 '22 19:09

bcr666