现在我正在 Spring 读取属性文件
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="customer/messages" />
</bean>
这里我指定读取客户目录中的 messages.properties。但我想要做的是指定一个目录并要求 spring 读取该目录中存在的所有属性文件。我怎样才能实现这一目标?
我尝试了 value="customer/*"但它不起作用。
最佳答案
使用<context:property-placeholder>
更推荐为:
<context:property-placeholder
locations="classpath:path/to/customer/*.properties" />
您还可以使用 Spring 3.1+ Java Config 来执行此操作:
@Bean
public static PropertyPlaceholderConfigurer properties(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[ ]
{ new ClassPathResource( "path/to/customer/*.properties" ) };
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
您可能需要定制资源类型以从中加载属性:
- Resource abstract implementations
- Built-in resource implementation
- A complete walk through of properties in Spring
要使用属性,您可以使用 Environment
抽象。它可以被注入(inject)并用于在运行时检索属性值。
关于java - 读取目录中的所有属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20733106/