spring 如何在spring上下文配置文件中定位properties文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18399366/
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
How to locate the properties file in the spring context configuration file
提问by hguser
I am using spring web mvc project, and I put all the spring related files under WEB-INF\spring, including a ormlite.xmland a jdbc.properties.
我使用的是spring web mvc项目,我把所有spring相关的文件都放在了下WEB-INF\spring,包括aormlite.xml和a jdbc.properties。
Now I want to locate the jdbc.propertiesfile in the ormlite.xml,Like this:
现在我想在 中找到该jdbc.properties文件ormlite.xml,如下所示:
<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>
But when I run the application ,it will told me that :
但是当我运行应用程序时,它会告诉我:
Could not load properties
Could not load properties
It can not find the properties file.
它找不到属性文件。
What is the problem?
问题是什么?
回答by hguser
From Spring forum:
来自Spring论坛:
The problem is that /WEB-INF isn't accessible as it isn't in the root of the path, you must use the same path as you use in your test case (include the src/main/webapp part but that will break your application from running).
I suggest you move the jdbc.properties to the src/main/resources directory and simply use classpath: prefix to load the properties.
Code:
<context:property-placeholder location="classpath:jdbc.properties"/>The code above assumes they are on the root of the classpath (which is where they are when they are in
src/main/resources).
问题是 /WEB-INF 不可访问,因为它不在路径的根目录中,您必须使用与您在测试用例中使用的路径相同的路径(包括 src/main/webapp 部分,但这会中断您的应用程序正在运行)。
我建议您将 jdbc.properties 移动到 src/main/resources 目录并简单地使用 classpath: 前缀来加载属性。
代码:
<context:property-placeholder location="classpath:jdbc.properties"/>上面的代码假设它们位于类路径的根目录(这是它们在 中时所在的位置
src/main/resources)。
I hope this can help someone else.
我希望这可以帮助别人。
回答by Torgeist
I had the same problem - property files outside the classpath.
我有同样的问题 - 类路径之外的属性文件。
My solution:
我的解决方案:
First define a properties bean:
首先定义一个属性bean:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/your.properties</value>
</property>
</bean>
Then reference it in the property-placeholder:
然后在属性占位符中引用它:
<context:property-placeholder properties-ref="configProperties" />
Works perfectly well for me!
对我来说效果很好!
回答by Paulius Matulionis
Instead of:
代替:
<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>
Use:
用:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/spring/jdbc.properties"/>
And your properties will be available in Spring file, and don't forget to add a namespace: xmlns:p="http://www.springframework.org/schema/p"
您的属性将在 Spring 文件中可用,并且不要忘记添加命名空间: xmlns:p="http://www.springframework.org/schema/p"
回答by Rob Blake
I think you're missing the prefix to instruct Spring how to attempt to load the properties. I think your definition needs to be:
我认为您缺少指示 Spring 如何尝试加载属性的前缀。我认为你的定义需要是:
<context:property-placeholder location="file:/WEB-INF/spring/jdbc.properties"/>
Note the addition of the file:prefix.
请注意添加了file:前缀。

