简体   繁体   English

读取目录中的所有属性文件

[英]Read all properties files in a directory

Right now I am reading properties file in spring as 现在我在春天读属性文件

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="customer/messages" />
</bean>

Here I am specifying that read messages.properties in customer directory. 在这里,我指定在客户目录中读取messages.properties。 But what I want to do is to specify a directory and ask spring to read all properties file present in that directory. 但是我要做的是指定一个目录,并要求spring读取该目录中存在的所有属性文件。 How can I achieve that? 我该如何实现?

I tried value="customer/*" but it doesn't work. 我尝试过value =“ customer / *”,但是不起作用。

Using <context:property-placeholder> is more recommended as: 推荐使用<context:property-placeholder>作为:

<context:property-placeholder 
    locations="classpath:path/to/customer/*.properties" />

You can also do this using Spring 3.1+ Java Config with: 您也可以使用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;
}

You may need to tailor the type of resource to load properties from: 您可能需要定制资源类型以从以下项加载属性:

To use a property, you can use Environment abstraction. 要使用属性,可以使用Environment抽象。 It can be injected and used to retrieve property values at runtime. 可以将其注入并用于在运行时检索属性值。

在以下博客上给出的最终使用方法-http: //rostislav-matl.blogspot.in/2013/06/resolving-properties-with-spring.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM