Search code examples
javaspringclasspathclassloader

How can I access resources on a parent classpath using Spring 2.5.x?


In my Websphere Portal environment, I'm trying to configure a bean that uses a resource found outside of the WAR (it's in some parent classpath for WAS). I'm sure it's there because I can access it using the following:

URL url = getClass().getResource("/config/someProps.properties");

However, in my Spring applicationContext.xml, the following does not work:

<bean id="initBean" class="foo.PropInitializer">
    <constructor-arg value="classpath:/config/someProps.properties"/>   
</bean>

If I remove the "classpath:", that doesn't help either.

Currently, I'm loading the Spring context using the ContextLoaderListener, but it seems that the parent classpath is not accessible/available to Spring.

Is there a way (using Spring config) to load the parent classpath as well?


Solution

  • I've managed to (sort of) solve my question.

    I ended up creating a factory class that I can use to load resources from the classpath:

    <bean name="applicationConfig" class="foo.io.ResourceLoader">
        <constructor-arg value="/config/someProps.properties" />
    </bean>
    

    This was based off of an existing Spring class: org.springframework.core.io.ClassPathResource

    Unfortunately, the Spring class didn't quite work in my situation (I was experiencing some property ambiguity issues in the method I was passing the resulting bean into) which is why I created my own class that returned the exact type.

    In any case, I expect that using Springs ClassPathResource will work in most situations.