views:

69

answers:

2

I'd like to load key-value pairs from multiple locations. My first guess was:

<util:properties id="requestProcessorRepository"
  location="classpath*:*requestProcessors.properties"/>

but it is not valid

Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classpath*:*requestProcessors.properties] cannot be opened because it does not exist
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:546)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    ... 24 more

Without wildcards, it starst to work.

So any other possibilities to create a properties object / map from wildcarded files?

A: 

classpath*: is not valid. Try classpath:**/requestProperties.properties to search the class path for files named "requestProperties.properties".

dwb
`classpath*:` is perfectly valid, it resolves to a list of `Resource` objects. The client class has to able to accept that list, though, which `<util:properties>` won't. In contrast, `classpath:**` will not work, since `classpath:` can only handle a single resource.
skaffman
+3  A: 

First of all, your resource path classpath*:*requestProcessors.properties is not reliable (from Spring Reference):

Please note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like "classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories.

If you solve this problem, you can load properties as follows:

<bean id = "requestProcessorRepository" 
    class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name = "locations" 
        value = "classpath*:somefolder/*requestProcessors.properties" />
</bean>

(Original version of the code was more complex, but there is no need in it, since Spring converts path with wildcards to Resource[] automatically).

axtavt
So should your solution load all files ending with `requestProcessors.properties` inside somefolder in the classpath root?
pihentagy
@pihentagy: Yes. I guess you can use your original path too if your classpath consists of expanded directories.
axtavt
Argh, I tried with `name="location"`, which should be `name="locations"`. Thanks for the answer
pihentagy