2014-06-11 45 views
0

我的項目有兩個模塊persistenceservices如何在類路徑中查找屬性文件?

持久性/ MySqlDatabaseConfig看起來像

@Configuration 
@Profile("default") 
@PropertySources({ 
     @PropertySource("classpath:resources/db.properties"), 
     @PropertySource(value = "file:/home/y/conf/pryme/db.properties", ignoreResourceNotFound = true) 
}) 
public class MySqlDatabaseConfig extends JpaCommonConfig { 
.... 
} 

服務/ src目錄/主/資源/ db.properties

database.driverClassName=com.mysql.jdbc.Driver 
database.url=jdbc:mysql://localhost:3306/newDB?createDatabaseIfNotExist=true 
database.username=root 
database.password= 

services模塊取決於persistence模塊

當我運行測試中,我看到錯誤的

Failed to load bean class: com.yahoo.comma.persistence.profiles.MySqlDatabaseConfig; nested exception is java.io.FileNotFoundException: class path resource [resources/db.properties] cannot be opened because it does not exist 

如何訪問性文件正確?

UPDATE
services.war,我看到它是在這裏

374 Wed Jun 11 11:26:16 PDT 2014 WEB-INF/classes/pryme.properties 
+0

屬性文件在哪裏?您的應用程序期望它位於名爲「resources」的子目錄中,該子目錄位於CLASSPATH中的子目錄內(子目錄是)。所以'資源'不需要在CLASSPATH中,但是它的父類可以。 – theglauber

+0

它不重複。該解決方案不適用於這種情況 – daydreamer

回答

1

src/main/resources是Maven使用到擁有資源的默認路徑。

在編譯/編譯期間,它將所有內容都放在那裏,並將它們放在目標運行時類路徑的根目錄下。

你需要指定條目

classpath:/db.properties 

,因爲它會在classpath的根目錄(/)。

+0

我仍然看到這個問題。你確定這將甚至在不同模塊中的屬性文件的情況下工作? – daydreamer

+0

@daydreamer你有兩個同名的文件嗎? –

+0

@daydreamer在Web應用程序中,「WEB-INF/classes」中的任何內容都被認爲是在類路徑的根目錄中。如果你的'db.properties'在那裏,檢索它的正確方法就是我所展示的。 –

相關問題