我会使用PropertySourcesPlaceholderConfigurer来读取我的系统属性。然后,您可以使用此语法来解析占位符 : ${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>
或者,使用 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;
}