【问题标题】:Spring Config Client XML equivalent of @RefreshScopeSpring Config Client XML 等效于 @RefreshScope
【发布时间】:2017-04-22 11:00:14
【问题描述】:

我们有一个现有的 Spring MVC 应用程序(非 Spring-boot 应用程序),所有(或大部分)bean 都在 XML 中配置。我们想将其用作 Spring Cloud Config Client(我们有一个 Spring Boot 应用程序充当配置服务器)。

在这方面,在 XML 中配置带有刷新范围的 bean 的 XML 等效项是什么(与 @RefreshScope 注释相同)。尝试将 RefreshScope 配置为 bean 并使用 scope="refresh" 但可以看到 bean 在执行 /refresh 端点(来自执行器)后没有反映新值

非常感谢您对此的任何帮助

【问题讨论】:

  • 核心问题是在 XML 中创建“可刷新”bean 的最佳方法是什么(相当于 @RefreshScope 注释)。

标签: spring spring-cloud-config


【解决方案1】:

正如其他答案中指出的那样,“刷新”范围只是另一个范围。但是,如果您在 XML 中定义和注入属性,则在 /refresh 调用之后没有使用新值更新 bean 属性。更多关于问题here。然而,bean(即实际上是代理)在每次 /refresh 调用后被实例化 - 但您需要“aop:scoped-proxy”配置,因为您注入“刷新”范围 bean 的 bean 可能是在不同的范围内。即

<bean name="xmlValueBean" class="me.fahimfarook.xml.XMLValueBean" scope="refresh">
    <aop:scoped-proxy proxy-target-class="true" />
</bean>

【讨论】:

    【解决方案2】:

    如果你想像人们已经指出的那样在核心 Spring(也是 Spring MVC)中使用@RefreshScope,你也必须自己实现范围。

    我也遇到了同样的困境,我也做过,我还写了一篇关于它的博客,你可以在那里找到所有的实现细节。

    如果您愿意,还可以将 Spring Boot 配置服务器与您的 Spring MVC 应用程序一起使用。

    @RefreshScope for Spring MVC

    【讨论】:

      【解决方案3】:

      @RefreshScope 只是另一个范围。看看RefreshScope 类是如何实现的。它正在创建一个名为“刷新”的新范围。

      这意味着您应该能够像这样在 XML 配置中使用新范围。

      <bean id = "..." class = "..." scope = "refresh">
      </bean>
      

      【讨论】:

      • using scope="refresh" throws java.lang.IllegalStateException: No Scope registered for scope name 'refresh'
      猜你喜欢
      • 2018-01-14
      • 2019-10-06
      • 2017-03-06
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 2018-01-27
      相关资源
      最近更新 更多