2012-10-01 45 views
0

我的屬性文件是以下路徑:無法加載屬性文件屬性佔位符

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

和我的spring配置文件路徑下:

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來構建我的項目,我失去了螞蟻的配置或日食配置這個問題?

螞蟻配置:

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

也我添加了META-INF文件夾在Eclipse部署組件。

請指教如何正確加載此屬性文件。

回答

3

你可以試試<context:property-placeholder location="classpath*:META-INF/app-config.properties" /> 如果這也失敗了,那麼可能是你的src \ main \資源不在classpath下。

0

我認爲問題出在您爲屬性文件定義的路徑中。 你可以試試下面的一個。

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

希望這可以幫助你。

+0

這是一樣的,產生相同的異常 –

+0

再試一件。 '' –

+0

它也不起作用。 –

0

在這裏稍微猜測一下,正如你所說的,你使用的是eclipse,但不是在發生異常時 - 從eclipse或命令行。如果在Eclipse中,那麼您需要將Eclipse項目設置與那些Ant使用的項目設置對齊。轉到Project-Properties下,而不是部署程序集,轉到Java Build路徑並驗證您的所有源輸入(代碼和資源)都在那裏。更重要的是,驗證輸出目錄設置爲Ant正在使用的相同的目錄。遠射,但值得一試!

0

屬性文件是否實際存在於已編譯的工件中?你能否向我們展示一個解壓縮的工件目錄列表,以便我們可以看到該文件是否存在?或者你是否從Eclipse運行它,並且出現錯誤?如果是這樣,您需要調整項目屬性以包含目錄和/或文件。