Spring : PropertyPlaceholderConfigurer cannot find property file

淺唱寂寞╮ 提交于 2019-12-01 06:22:07

问题


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,


回答1:


try classpath*:jdbc.properties




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!