3

I have a strange problem with Spring using PropertyPlaceholderConfigurer. One of my beans is designed as follow :

<bean name="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>

The problem is that spring never find jdbc.properties (FileNotFoundException). The file is in a folder named "resources" that is in the bundle classpath (I am working in a OSGi project).

I have tried almost every combination ("jdbc.properties", "/jdbc.properties", "classpath:jdbc.properties", "classpath:/jdbc.properties", "/resources/jdbc.properties", etc...) but it never works.

For information, if at some point, I do something like :

URL u = someClassLoader.getResource("jdbc.properties");

it does work without any problem and find the file. Actually I am totally unable to understand what is the bug with spring.

If you have any idea to help me, thanks in advance. I am not very experienced in spring so I have maybe done a mistake somewhere.

[EDIT]

Actually, it's a problem of classloader : If I do :

new ClassPathResource("jdbc.properties");

it doesn't work. But :

new ClassPathResource("jdbc.properties",someClassIntheBundle.class.getClassLoader());

works perfectly.

I do believe that Spring use the ClassLoader of its own bundle that is consumed by my bundle. Do you know a way to solve this tricky problem ?

Thanks,

ThR37
  • 3,965
  • 6
  • 35
  • 42
  • For the moment, I have to put awful static path in my config-file for testing (like bundleresource://63.jhdjlzeo:2/jdbc.properties (it works with it)). I haven't find any other solution (cf. other comments infra) – ThR37 Aug 26 '11 at 14:02

2 Answers2

1

try classpath*:jdbc.properties

Alexey Kutuzov
  • 679
  • 8
  • 22
  • Actually, it does not complain anymore but it does not find the location as well :s Thanks in all cases for your help. Do you know what is the main difference with classpath: ? – ThR37 Aug 26 '11 at 13:50
  • Sorry about long answer. You can find it in spring [references](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html). And, of course you can create sub-class of PropertyPlaceholderConfigurer and load prop files using `MyConfigurer.class.getResourceAsStream()`. – Alexey Kutuzov Aug 31 '11 at 15:23
1

IANA OSGI developer, but a quick Google search results in a link to the Spring-osgi documentation. Look at section 5.4 and note that the spring-osgi package makes some changes to Resource loading. It looks like the ResourceLoader implemented by the default ApplicationContext for osgi will automatically pre-pend osgibundle: if no other prefix is provided.

It appears as though there is some difference in scope between the path searched when using classpath: and the path used when using classpath*:, but I have so far been unable to find a good explanation for it.

Ryan Ransford
  • 3,224
  • 28
  • 35
  • Thanks for the link. I had probably missed it. There are some "strange" things that I need to investigate in all cases (with classpath*:, it does find the property file but then it's still unable to resolve placeholders. It may not be linked at all but I am not sure... – ThR37 Aug 25 '11 at 14:24
  • @ThR37: I'm unable to find documentation for it currently, but I do seem to remember that there was a shortcoming in property token resolution when properties loaded in a `PropertyPlaceholderConfigurer` in a parent `ApplicationContext` were referenced from an `import`ed `ApplicationContext`. – Ryan Ransford Aug 25 '11 at 14:35