使用@Value注释注入日期

时间:2013-08-05 19:47:55

标签: spring date annotations code-injection

这就是我试图在@Component类中注入日期的方法。

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

日期在属性文件中指定为String:

PROP_DATE=20110421

我收到此错误。我究竟做错了什么?有没有其他方法来注入Date属性?感谢。

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")'

1 个答案:

答案 0 :(得分:9)

使用#{new java.text.SimpleDateFormat(\"yyyyMMdd\").parse(\"${PROP_DATE}\")}将过程值与spEL

一起使用
相关问题