【问题标题】:Can't use @Value and @Autowired with properties in Spring Cloud Configuration Server不能在 Spring Cloud Configuration Server 中将 @Value 和 @Autowired 与属性一起使用
【发布时间】:2018-04-22 11:24:51
【问题描述】:

我有一个使用 Edgware.SR3 Spring Cloud 版本的 SpringBoot 1.5.12 应用程序。 以下代码:

@Configuration
public class HmlConfig
{
    @Value("${jms.destination.name}")
  ...
}
...
@RestController
@RequestMapping("/api")
public class HmlRestController
{
    @Autowired
    private JmsTemplate jmsTemplate;
  ...
}

引发以下异常:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jms.destination.name' in value "${jms.destination.name}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)

这是我的 bootstrap.yml 内容:

spring:
  application:
    name: hml-core
  profiles:
    active:
      default
  cloud:
    config:
      uri: http://localhost:8888/hml

转到http://localhost:8888/hml/hml-core/default 可以正确显示属性。我错过了什么吗?

【问题讨论】:

  • 这里的人们似乎更关心重新格式化问题而不是回答问题。我在这个论坛上发帖已经有一段时间了,但我从来没有得到我的问题的相关答案。但是,我未回答的问题现在格式很好。

标签: spring-cloud-config


【解决方案1】:

根据您的日志,您的客户端无法访问配置服务器,这就是无法注入字段 jms.destination.name 的原因。

添加客户端的完整堆栈跟踪会有所帮助

【讨论】:

  • 感谢您花时间回答我的帖子。是的,我知道客户端无法访问配置服务器,这正是我在帖子中解释的内容。问题是为什么以及需要做什么才能使客户端可以访问配置服务器。你有什么主意吗 ?如果是,请不要犹豫,不吝赐教。如果没有,再次感谢您的参与。无论如何,堆栈跟踪清楚地表明没有找到给定的属性。如果我将它添加到本地属性文件中,则会找到它。如前所述,配置服务器可以正常工作,因为我能够显示属性。
  • 添加完整的堆栈跟踪将帮助其他人检测何时失败。客户端做的第一件事是连接到配置服务器,如果失败,堆栈跟踪会显示原因(例如,未授权、未找到或其他)。其次,也许您应该考虑添加您的客户端依赖项。任何信息都有助于解决问题。
【解决方案2】:

您是否碰巧在某处设置了spring.main.sources

我们遇到了完全相同的问题,删除该行对我们有帮助。似乎在 bootstrap.yml 中有这个会阻碍应用程序连接到云配置服务器。这会导致缺少道具。

在配置服务器端还有spring.main.sources 参数导致服务器端异常。见Spring cloud config /refresh crashes when spring.main.sources is set

【讨论】:

    【解决方案3】:

    我正在用上次测试的结果更新这篇文章。似乎在每个模块中包含 Maven 依赖项可以解决问题,然后按预期找到配置。 在我最初的设计中,我有一个父 POM,它分解了所有 Spring Boot 依赖项,如下所示:

      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR5</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-rsa</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    

    这样配置客户端找不到配置服务器并引发上述异常。修改父 POM 使其仅包含dependencyManagement,如下所示:

      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR5</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    

    并在配置服务器 POM 中移动依赖项,如下所示:

    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
    </dependencies>
    

    在配置客户端中如下:

      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-rsa</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    

    完全解决了问题,一切都按预期工作。我对此没有任何解释,因为在父 POM 中包含所有常见的依赖项应该与将它们包含在每个单独的模块中具有完全相同的效果。在我看来,这显示了 Spring Boot 和 Spring Cloud 的一些不稳定性和奇怪的行为。我没有时间深入挖掘并试图了解发生了什么。无论如何,鉴于此类问题,以及缺乏任何支持,包括本网站,我们从 Spring 迁移。 但如果有人对此有任何解释,我仍然有兴趣知道。 亲切的问候, 尼古拉斯

    【讨论】:

    • 最后我可以确定具有以下依赖关系:codeorg.springframework.cloudspring-cloud-config-server父 POM 中的 引发“java.lang.IllegalArgumentException:无法解析占位符...”。通过简单地将其从父 POM 移动到配置服务器模块 POM,异常消失并且一切都按预期工作。大混乱!
    猜你喜欢
    • 2018-02-12
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2016-04-03
    • 2016-02-23
    • 2017-05-17
    相关资源
    最近更新 更多