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,
try classpath*:jdbc.properties
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.
来源:https://stackoverflow.com/questions/7191162/spring-propertyplaceholderconfigurer-cannot-find-property-file