Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在类路径上查找属性文件?_Java_Spring - Fatal编程技术网

Java 如何在类路径上查找属性文件?

Java 如何在类路径上查找属性文件?,java,spring,Java,Spring,我的项目有两个模块persistence和services 持久性/MySqlDatabaseConfig看起来像 @Configuration @Profile("default") @PropertySources({ @PropertySource("classpath:resources/db.properties"), @PropertySource(value = "file:/home/y/conf/pryme/db.properties", ign

我的项目有两个模块
persistence
services

持久性/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/main/resources/db.properties

database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/newDB?createDatabaseIfNotExist=true
database.username=root
database.password=
另外,
服务
模块依赖于
持久性
模块

当我运行测试时,我将错误视为

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

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

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

您需要将条目指定为

classpath:/db.properties

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

属性文件在哪里?应用程序希望它位于名为“resources”的子目录中,该子目录位于类路径中的子目录中。所以
resources
不需要在类路径中,但它的父级需要在类路径中。它不重复。这个解决方案不适用于这种情况,我仍然看到这个问题。您确定这在不同模块中的属性文件中也会起作用吗?@daydreamer您有两个同名文件吗?@daydreamer在WEB应用程序中,
WEB-INF/classes
中的任何内容都被视为在类路径的根目录中。如果您的
db.properties
在其中,那么正确的检索方法就是我所展示的。