【问题标题】:How can I access 'spring.application.name' when defined in bootstrap.properties?在 bootstrap.properties 中定义时如何访问“spring.application.name”?
【发布时间】:2017-04-30 12:39:03
【问题描述】:

我有以下 spring-boot 1.4.2.RELEASE 示例应用

@SpringBootApplication
public class Application {

    @Value("${spring.application.name}")
    private String applicationName;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我在 bootstrap.properties 中定义了以下配置:

spring.application.name=sample-app

运行时出现以下错误:

java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in string value "${spring.application.name}"

关于为什么它无法注入“spring.application.name”的任何提示? 需要在那里定义它以支持其他spring boot cloud。

【问题讨论】:

  • 此应用程序中还有其他问题。如果你把它放在bootstrap.properties 并且你确实使用了spring cloud,它就会可用。我最好的猜测是 bootstrap.properties 在您的应用程序中被完全忽略。也许你有云部门但你没有使用它?
  • 是的,你是对的。我本来打算使用 spring-cloud,但在阅读了您的评论后,我意识到我还没有使用它!谢谢!

标签: java spring spring-boot dependency-injection


【解决方案1】:

第一个答案是正确的。默认属性文件是 application.properties 或 application.yml。

引导文件适用于 Spring Cloud。

http://projects.spring.io/spring-cloud/spring-cloud.html#_the_bootstrap_application_context

如果您使用的是spring cloud,并且引导文件不起作用,则需要启用“云”Spring配置文件。

例如使用:

./gradlew -Dspring.profiles.active=cloud bootrun 

./mvnw spring-boot:run -Dspring.profiles.active=cloud

【讨论】:

    【解决方案2】:

    默认情况下,如果你不指定任何属性来源,spring boot 会在文件application.properties 中查找你的属性。因此,您应该将您的属性文件重命名为该默认名称或手动为您的 bootstrap.properties 指定一个属性源

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多