Spring:如何从属性文件设置@DateTimeFormat的模式?

Spring:如何从属性文件设置@DateTimeFormat的模式?,spring,model,controller,annotations,datetime-format,Spring,Model,Controller,Annotations,Datetime Format,我使用的是Spring3.1.1.RELEASE。我有一个模型,我提交给我的一个控制器。在其中,是以下字段 @DateTimeFormat(pattern = "#{appProps['class.date.format']}") private java.util.Date startDate; 但是,上述方法不起作用(EL没有被解释),因为每次我提交表单时,都会出现一个错误。如果我使用下面的 @DateTimeFormat(pattern="yyyy-MM-dd") private jav

我使用的是Spring3.1.1.RELEASE。我有一个模型,我提交给我的一个控制器。在其中,是以下字段

@DateTimeFormat(pattern = "#{appProps['class.date.format']}")
private java.util.Date startDate;
但是,上述方法不起作用(EL没有被解释),因为每次我提交表单时,都会出现一个错误。如果我使用下面的

@DateTimeFormat(pattern="yyyy-MM-dd")
private java.util.Date startDate;
一切正常。但理想情况下,我希望从属性文件驱动模式。这可能吗?如果可能,正确的语法是什么

  • 戴夫

目前,它似乎只适用于属性占位符

看看这个:

我会用它来读取我的系统属性。然后可以使用此语法解析占位符:
${prop.name}

您的带注释的字段应如下所示:

@DateTimeFormat(pattern = "${class.date.format}")
private java.util.Date startDate;
要在xml中为应用程序配置PropertySourcesPlaceholderConfigurer,请尝试以下操作:

<bean class="org.springframework.beans.factory.config.PropertySourcesPlaceholderConfigurer">
  <property name="location">
    <list>
      <value>classpath:myProps.properties</value>
    </list>
  </property>
  <property name="ignoreUnresolveablePlaceholders" value="true"/>
</bean>
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    //note the static method! important!!
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[] { new ClassPathResource("myProps.properties")};
    configurer.setLocations(resources);
    configurer.setIgnoreUnresolvablePlaceholders(true);
    return configurer;
}