【问题标题】:spring context.refresh() and concurrent accessspring context.refresh() 和并发访问
【发布时间】:2011-11-29 11:47:29
【问题描述】:

我打算使用 spring 作为我们企业应用程序的配置服务。

这部分在 spring 参考指南和其他博客中有很好的记录。基本上使用属性文件和 context:property-placeholder 我计划在 bean 上实例化和填充值,然后由应用程序使用。

在某些情况下,属性文件会发生更改,在这种情况下,我希望 bean 反映更改后的值。我知道 ApplicationContext.refresh() 是刷新 bean 及其配置值的方法。 ApplicationContext.refresh() 就像一个魅力。

<context:property-override location="file:///d:/work/nano-coder/quickhacks/src/main/resources/myproperties.properties"/>

<bean id="myconfig" class="org.nanocoder.quickhacks.config.MyPropertyBean">
    <property name="cacheSize" value="${registry.ehcache.size}"/>
    <property name="httpHostName" value="${url.httpHostName}"/>
    <property name="httpsHostName" value="${url.httpsHostName}"/>
    <property name="imageServers">
        <list>
          <value>${url.imageserver1}</value>
          <value>${url.imageserver2}</value>
          <value>${url.imageserver3}</value>
        </list>
    </property>

</bean>

然而在刷新上下文时,我发现并发调用 ApplicationContext.getBean() 或对 bean 的任何 getter 操作可能会由于 IllegalStateExceptionBeanCreationException

value = context.getBean("myconfig", MyPropertyBean.class).getHttpHostName();

问题

  1. 对 context.refresh() 的调用是否可能不会影响其他并发调用
  2. 如果 context.refresh() 可以中断对应用程序上下文的并发访问,是否有任何策略可以避免这种情况发生。

您的指点将不胜感激。

【问题讨论】:

    标签: spring configuration refresh applicationcontext


    【解决方案1】:

    你可以做的是围绕你的配置服务创建一些包装器,而不是刷新现有的上下文创建一个新的。当新的准备好后,开始使用它而不是旧的。

    我不确定这是否是配置管理的最佳选择,但代码可能看起来像这样(稍后至少可以引入一个不依赖 spring 的接口):

    class MyConfig {
      private ApplicationContext context;
    
      public ApplicationContext getContext() {
        return context;
      }
    
      public void refresh() {
        context = new FileSystemXmlApplicationContext(..)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2012-07-21
      • 1970-01-01
      相关资源
      最近更新 更多