2013-07-31 30 views
1

我使用Spring 3.1.1.RELEASE。我有一个模型,我提交给我的一个控制器。在这里面,是以下领域Spring:如何从属性文件中设置@DateTimeFormat的模式?

@DateTimeFormat(pattern = "#{appProps['class.date.format']}") 
private java.util.Date startDate; 

然而,上述不工作(在EL不理解),在尽可能每次我提出我的表格时,我得到一个错误。如果我使用以下

@DateTimeFormat(pattern="yyyy-MM-dd") 
private java.util.Date startDate; 

一切工作正常。但理想情况下,我想从属性文件中驱动该模式。这是可能的,如果是这样,什么是正确的语法?

  • 戴夫

回答

0

我会用PropertySourcesPlaceholderConfigurer阅读我的系统性能。然后,您可以使用此语法来解析占位符:${prop.name}

注释的申请应该像这样,那么:

@DateTimeFormat(pattern = "${class.date.format}") 
private java.util.Date startDate; 

要配置PropertySourcesPlaceholderConfigurer在XML应用程序,试试这个:

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

或者与JavaConfig:

@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; 
} 
相关问题