无法加载property-placeholder的属性文件

时间:2012-10-01 08:19:35

标签: spring ant eclipse-juno

我的属性文件位于以下路径:

src\main\resources\META-INF\app-config.properties

我的弹簧配置文件位于路径下:

WebContent\WEB-INF

并在尝试按如下方式加载属性文件时:

<context:property-placeholder
        location="classpath:META-INF/app-config.properties" />

我得到例外:

java.io.FileNotFoundException: class path resource [META-INF/app-config.properties] cannot be opened because it does not exist

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <import resource="application-Security-Context.xml" />

    <import resource="application-DataAccess-Context.xml" />

    <import resource="application-Service-Context.xml" />

    <context:component-scan base-package="com.myapp" />

    <!-- PERSISTENCE -->
    <context:property-placeholder
        location="classpath:META-INF/app-config.properties" />


    <jee:jndi-lookup id="appDS" jndi-name="MyApp" expected-type="javax.sql.DataSource"/>

    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultDataSource" ref="appDS" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
        <property name="persistenceUnitName" value="MyApp" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>


    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

applicationContext在web.xml中加载如下:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

我正在使用ant来构建我的项目,我是否缺少针对此问题的ant配置或eclipse配置?

ant config:

<zipfileset dir="${basedir}/src/main/resources/META-INF" includes="app-config.properties" fullpath="WEB-INF/classes/META-INF/app-config.properties"/>

我还在eclipse中将META-INF文件夹添加到部署程序集中。

请告知如何正确加载此属性文件。

4 个答案:

答案 0 :(得分:3)

你可以尝试<context:property-placeholder location="classpath*:META-INF/app-config.properties" />  如果这也失败,则可能是你的src \ main \ resources不在classpath下。

答案 1 :(得分:0)

我认为问题出在为属性文件定义的路径中。 你能试试下面的吗?

<context:property-placeholder location="classpath:/META-INF/app-config.properties" />

希望这会对你有所帮助。

答案 2 :(得分:0)

在这里进行一些猜测,正如你所说的那样,你正在使用eclipse,但不是在异常发生的时候 - 从eclipse或命令行。如果来自Eclipse,那么您将需要将Eclipse项目设置与Ant正在使用的项目设置对齐。转到Project-Properties,但不是部署程序集,转到Java Build路径并验证所有源输入(包括代码和资源)是否存在。更重要的是,验证输出目录是否设置为Ant正在使用的相同目录。远射,但值得一试!

答案 3 :(得分:0)

属性文件实际上是在您编译的工件中吗?你能告诉我们你的解压缩工件的目录列表,以便我们看看文件是否存在?或者您是否正在从Eclipse运行它并且出现错误?如果是这样,您需要调整项目属性以包含目录和/或文件。