4

我正在使用spring web mvc项目,我把所有的spring相关文件都放在了WEB-INF\spring,包括aormlite.xml和a jdbc.properties

现在我想在 中找到jdbc.properties文件ormlite.xml像这样

<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>

但是当我运行应用程序时,它会告诉我:

Could not load properties

它找不到属性文件。

问题是什么?

4

4 回答 4

9

来自春季论坛:

问题是 /WEB-INF 不可访问,因为它不在路径的根目录中,您必须使用与测试用例中使用的路径相同的路径(包括 src/main/webapp 部分,但这会中断您的应用程序从运行)。

我建议您将 jdbc.properties 移动到 src/main/resources 目录并简单地使用 classpath: 前缀来加载属性。

代码:

<context:property-placeholder location="classpath:jdbc.properties"/>

上面的代码假定它们位于类路径的根目录上(当它们位于 时,它们就在其中src/main/resources)。

我希望这可以帮助别人。

于 2013-08-24T02:32:04.670 回答
1

我有同样的问题 - 类路径之外的属性文件。

我的解决方案:

首先定义一个属性bean:

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">
            <value>/WEB-INF/your.properties</value>
        </property>
    </bean>

然后在属性占位符中引用它:

<context:property-placeholder properties-ref="configProperties" />

对我来说效果很好!

于 2014-02-10T10:48:05.493 回答
0

代替:

<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>

利用:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/spring/jdbc.properties"/>

您的属性将在 Spring 文件中可用,并且不要忘记添加命名空间:xmlns:p="http://www.springframework.org/schema/p"

于 2013-08-23T09:31:00.683 回答
0

我认为您缺少指示 Spring 如何尝试加载属性的前缀。我认为您的定义需要是:

<context:property-placeholder location="file:/WEB-INF/spring/jdbc.properties"/>

注意文件的添加:前缀。

于 2013-08-23T10:10:35.157 回答