2014-06-11 48 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'在那里,检索它的正确方法就是我所展示的。 –

相关问题