java Spring:PropertyPlaceholderConfigurer 找不到属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7191162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:00:33  来源:igfitidea点击:

Spring : PropertyPlaceholderConfigurer cannot find property file

javaspringpropertiesclasspathosgi

提问by ThR37

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

我在使用 Spring 时遇到了一个奇怪的问题PropertyPlaceholderConfigurer。我的一个豆子设计如下:

<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).

问题是 spring 永远找不到 jdbc.properties( FileNotFoundException)。该文件位于包类路径中名为“resources”的文件夹中(我在 OSGi 项目中工作)。

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

我尝试了几乎所有组合(“jdbc.properties”、“/jdbc.properties”、“classpath:jdbc.properties”、“classpath:/jdbc.properties”、“/resources/jdbc.properties”等... ) 但它永远不会奏效。

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.

它确实可以正常工作并找到文件。实际上,我完全无法理解 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 ?

我确实相信 Spring 使用自己的包的 ClassLoader,它被我的包消耗。你知道解决这个棘手问题的方法吗?

Thanks,

谢谢,

回答by Alexey Kutuzov

try classpath*:jdbc.properties

尝试 classpath*:jdbc.properties

回答by Ryan Ransford

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

IANA OSGI 开发人员,但在 Google 上快速搜索会找到指向Spring-osgi 文档的链接。查看 5.4 节并注意 spring-osgi 包对Resource加载进行了一些更改。如果没有提供其他前缀,osgiResourceLoader的默认实现看起来ApplicationContext会自动预先挂起osgibundle:

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.

似乎classpath:使用时搜索的路径和使用时使用的路径之间的范围存在一些差异classpath*:,但到目前为止我无法找到一个很好的解释。