简体   繁体   中英

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. But what I want to do is to specify a directory and ask spring to read all properties file present in that directory. How can I achieve that?

I tried value="customer/*" but it doesn't work.

Using <context:property-placeholder> is more recommended as:

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

You can also do this using Spring 3.1+ Java Config with:

@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. It can be injected and used to retrieve property values at runtime.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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