如何在classpath上找到属性文件?

时间:2014-06-11 18:32:09

标签: java spring

我的项目有两个模块persistenceservices

persistence / 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 {
 ....
}

services / src / main / resources / 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  

如何正确访问属性文件?

更新
services.war中,我看到它就在这里

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

1 个答案:

答案 0 :(得分:1)

src/main/resources是maven用来保存资源的默认路径。

在编译/构建期间,它会将所有内容放在那里并将它们放在目标运行时类路径的根目录下。

您需要将条目指定为

classpath:/db.properties

因为它将位于类路径的根(/)。