Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject date using @Value annotation

This is how I am trying to inject a date into a @Component class.

@Value("${new java.text.SimpleDateFormat(\"yyyyMMdd\").parse(\"${PROP_DATE}\")}")
Date myDate;

The date is specified as a String in properties file :

PROP_DATE=20110421

I get this error. What am I doing wrong? Is there any other way to inject Date property? Thanks.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ccc': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.util.Date aaa.bbb.ccc.myDate; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'new java.text.SimpleDateFormat("yyyyMMdd").parse("20110421")'
like image 659
sharma sharma Avatar asked Dec 11 '22 12:12

sharma sharma


1 Answers

Use #{new java.text.SimpleDateFormat(\"yyyyMMdd\").parse(\"${PROP_DATE}\")} for process value with spEL

like image 58
Luca Basso Ricci Avatar answered Dec 28 '22 08:12

Luca Basso Ricci