使用 Spring PropertySource 找不到属性

Art*_*hur 4 java spring annotations properties filenotfoundexception

配置

@Configuration
@PropertySources({
    @PropertySource("classpath*:properties/test-database.properties")
})
public class DataSourceConfiguration {//...
}
Run Code Online (Sandbox Code Playgroud)

道具位置

D:\Projects\opti\dao\src\main\resources\properties\test-database.properties
Run Code Online (Sandbox Code Playgroud)

D:\Projects\opti\dao\src\main\resources 标记为资源文件夹。

小智 5

为了避免此类问题,问题是在VM 参数中设置 jboss.server.config.dir,如下所示:

\n\n
-Djboss.server.config.dir="[jboss_repository]/server/[default-all-standard-standalone]/conf" \xe2\x80\x93server\n
Run Code Online (Sandbox Code Playgroud)\n\n

你像这样设置 PropertySource :

\n\n
@Configuration\n@PropertySource("file:${jboss.server.config.dir}/file.properties")\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者你这样设置你的属性

\n\n
@PropertySource(value = "classpath:application.properties")\n
Run Code Online (Sandbox Code Playgroud)\n\n

执行时,将从位于类路径根目录中的application.properties文件导入属性。

\n