我正在用gradle开发一个spring引导应用程序。
我想告诉spring在哪里使用参数读取.properties文件。因为我仍然通过gradle运行它,所以我将它添加到我的build.gradle中
bootRun {
args = [
"--spring.config.additional-location=file:/path/to/my/props/folder/,file:/path/to/another/props/folder/"
]
}在/path/to/my/props/folder/中,我创建了一个文件remote-connection.properties
### remote-connection
remote.ip.address=127.0.0.1
remote.ip.port=5001我正试着像这样装那些道具
@RestController
@PropertySource("file:remote-connection.properties")
public class MyController {
@Value("${remote.ip.address}")
private String remoteIpAddress;
}当我运行./gradlew bootRun时,我有以下错误
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [my.package.MyApplication]; nested exception is java.io.FileNotFoundException: remote-connection.properties (No such file or directory)(我也尝试过@PropertySource("classpath:remote-connection.properties")和@PropertySource("remote-connection.properties"))
如果我将remote-connection.properties放置到src/main/resources中,它将运行得非常完美,但我希望配置文件位于结果jar之外,能够使用
java -jar my-application.jar --spring.config.additional-location=file:/path/to/my/props/folder/,file:/path/to/another/props/folder/我遗漏了什么?
提前谢谢。
发布于 2022-08-24 10:55:33
回答我自己的问题。
按照本指南https://mkyong.com/spring/spring-propertysources-example/,我已将运行args更改为
bootRun {
args = [
"--my.props.folder=/path/to/my/props/folder",
"--my.other.props.folder=/path/to/another/props/folder",
]
}像这样装道具
@RestController
@PropertySource("file:${my.props.folder}/remote-connection.properties")
@PropertySource("file:${my.other.props.folder}/some-more.properties")
public class MyController {
@Value("${remote.ip.address}")
private String remoteIpAddress;
}这样,就行了!!
https://stackoverflow.com/questions/73462443
复制相似问题