【发布时间】:2019-03-23 10:16:30
【问题描述】:
我的gradle.properties 文件中有一些数据:
appVersion=1.4.31
appGroup=com.business
appName=projectName
我希望能够在.java 项目中访问它们。我正在尝试像这样访问变量:
@Value("${appVersion}")
private int version;
但是,当我尝试从主类运行应用程序时,出现以下错误:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
这表明它找不到正确的变量。
解决问题的尝试
添加@propertySource注解
我已经尝试将它添加到主类
@PropertySource(value = "file:build.gradle", ignoreResourceNotFound = true) 失败了。
更改build.gradle
我尝试了以下内容,取自 this StackOverflow 答案:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
这也失败了。
This下面回答
我做了这个答案中所说的(我必须添加一个application.properties 文件)并且我收到了这个错误消息:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Circular placeholder reference 'appVersion' in property definitions
我认为它可能会与使用相同的名称 (appVersion = appVersion) 混淆,因此我将 application.properties 行更改为
version=${appVersion}
然后在我的.java 文件中,我将行更改为:
@Value("${version}")
private String version;
得到了错误:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
【问题讨论】:
-
如何启动应用程序:从 IDE (eclipse/IDEA)?或来自 gradle 任务(运行或 bootRun)?
-
This 可能会有所帮助。
-
我从 IDEA 启动项目。
-
application.properties文件应该已经存在。好吧,除非您没有使用引导初始化程序,否则我想。你把它放在src/main/resources了吗? -
在从 Idea 启动您的应用之前尝试运行 gradle 进程资源任务
标签: java spring spring-boot gradle