我的项目有两个模块persistence
和services
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
答案 0 :(得分:1)
src/main/resources
是maven用来保存资源的默认路径。
在编译/构建期间,它会将所有内容放在那里并将它们放在目标运行时类路径的根目录下。
您需要将条目指定为
classpath:/db.properties
因为它将位于类路径的根(/
)。