2013-10-14 34 views
0

春找不到的src /主/資源在我的屬性文件(MyPropFile.properties)春找不到的src/main /資源的內部屬性文件並拋出類似的異常以下Maven項目:使用PropertyPlaceholderConfigurer

java.io.FileNotFoundException: class path resource [file*:/src/main/resources/MyPropFile.properties] cannot be opened because it does not exist 

但如果我將MyPropFile.properties放置在項目的根目錄(MyProject/MyPropFile.properties)中,spring可以找到它並且程序正確執行。

如何配置,這樣我可以把我的.properties的src/main /資源內部文件

這是我的命名空間

<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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    "> 

這是我的豆

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

Java:

@Value("${message.fromfile}") 
     private String message; 

在此先感謝你們。

回答

2

試試這個。使你的應用程序配置文件此項:

<beans xmlns:context="http://www.springframework.org/schema/context" 
..... 
xsi:schemaLocation="... 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
....> 

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

和訪問消息屬性:

@Value("${messageFromfile}") 
private String message; 
+0

我已經更新了我的問題,反映了你建議的先生的更改 不幸的是,它仍然無法找到屬性文件 java.io.FileNotFoundException:MyPropFile.properties(系統找不到文件指定) – royjavelosa

+0

註釋'PropertyPlaceholderConfigurer'的bean定義並嘗試構建並運行你的應用程序 –

+0

只有當MyPropFile.properties位於我的項目的根目錄下時,它纔會工作我希望它可以工作,即使它在src/main/resources/MyPropFile.properties – royjavelosa

1

我期望Maven將其複製到目標目錄並且適當地設置類路徑。我不希望Maven在編譯時搜索源目錄目錄。

+0

你是什麼意思?它在eclipse中運行良好,如果我把屬性文件放在項目的根目錄。我的問題是當我把屬性文件放在src/main/resources中,它被假定爲maven項目的默認資源位置。 – royjavelosa

1

您應該使用

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
      <value>classpath:/MyPropFile.properties</value> 
     </property> 
</bean> 

沒有classpath:前綴,則PropertyPlaceholderConfigurer試圖解決資源作爲文件資源,因此在當前工作目錄下尋找它。

+0

剛剛嘗試過,但它給出了這個錯誤。 順便說一句,我使用Spring 3.0.5.RELEASE java.io.FileNotFoundException:MyPropFile.properties(系統找不到指定的文件) \t at java.io.FileInputStream.open(Native Method) – royjavelosa

+0

您可以驗證是否存在「{project directory}/target/classes/MyPropFile.properties」? – ryanp

+0

不幸的是,它在那裏,我檢查了很多次:( – royjavelosa

0

作爲附錄@ryanp解決方案,它是處理文件資源的正確和消毒方法,這應該在類路徑的位置下。

Maven會自動地收集配置文件資源,並把它們添加到運行時類路徑持有使資源前綴類路徑:可以解決。

否則,如果發現使用Maven不能夠流文件資源,你的自我堆棧,你可以掛接到Maven的資源配置和映射文件的位置如下:

<build> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <includes> 
       <include>*.properties</include> 
      </includes> 
     </resource> 
    </resources> 
</build>