【问题标题】:Could not resolve placeholder 'spring.profiles.active' in value "classpath:application-${spring.profiles.active}.properties"无法解析值“classpath:application-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”
【发布时间】:2020-06-03 16:13:42
【问题描述】:

虽然我的问题有类似的问题和答案,但 nomn 有帮助,所以我在这里发帖。

所以我有 env 特定的属性文件,比如 -

application.properties
application-local.propereties
application-dev.propereties
application-test.propereties
application-stage.propereties

然后在我的 AppCpnfig 类中我使用了

@PropertySource("classpath:application-${spring.profiles.active}.properties")

我正在使用 intellij,在运行配置中我设置了本地。 我的应用程序是 maven 项目。

在所有属性文件(application.properties 除外)中,我还添加了以下条目来设置特定的环境。例如 -

 spring.profiles.active=local
 -Dspring.profiles.active=local

但是当我构建我的应用程序时仍然遇到这个问题 -

但是,当我构建此应用程序时,它会因问题而失败 - 无法解析值中的占位符“spring.profiles.active”

classpath:application-${spring.profiles.active}.properties

如果有人知道解决此问题的方法,请提供帮助。

【问题讨论】:

  • 你能用#代替$吗?
  • 不要... Spring Boot 已经为您加载了,您只是在针对框架工作。删除@PropertySource,Spring Boot 会处理剩下的事情。

标签: spring spring-boot spring-profiles


【解决方案1】:

您不应在属性文件中使用 -Dspring.profiles.active=local 进行输入,而应在运行 jar 文件时从外部传递它,例如:

java -jar -Dspring.profiles.active=[yourProfileName] [jarFilePath].jar

如果找不到文件,您也可以克服该问题:

@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)

【讨论】:

  • 他一开始就不应该这样做。 Spring Boot 已经处理了所有这些。使用框架而不是围绕/反对它。
  • 但她没有提到她正在使用 springboot 和它的框架工具来处理丢失的文件。在springboot的情况下,她可以直接使用@Value,框架会根据env和属性文件名自动收集属性值。例如:对于开发环境 application-dev.properties 将被拾取。
  • 它在标签列表中。
  • 能否请您突出显示,因为我无法识别。
  • 每个问题都属于 Spring 旁边的一个组(又名标签),Spring Boot 就是其中一个标签。
猜你喜欢
  • 2019-01-27
  • 2023-02-24
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
相关资源
最近更新 更多