【问题标题】:Automatic configuration reinitialization in SpringSpring中的自动配置重新初始化
【发布时间】:2010-10-02 11:32:09
【问题描述】:

在 Log4j 中,有一个功能可以初始化系统以执行配置和间隔监视。这允许 log4j 系统在属性文件更改时重新加载其属性。 spring 框架是否有这样的 Configuration Observer 工具,其中 Configuration 在更改时重新加载。需要重新加载的配置不是 Springs 的 applicationContext.xml,而是使用 Spring 初始化 bean 初始化的各种其他配置文件。

【问题讨论】:

    标签: spring


    【解决方案1】:

    AFAIK Spring 不提供这样的实用程序。但是,有一个第 3 方工具 JRebel 可以让您更新整个 Web 应用程序(包括 Spring 配置),而无需重新启动服务器。

    提供免费试用,购买价格相当便宜。

    【讨论】:

      【解决方案2】:

      我发现了一个类似于 Log4J here 的实用程序。它基本上是 PropertyPlaceholderConfigurer 的扩展,可在属性更改时重新加载。

      【解决方案3】:

      在重新加载 spring 应用程序上下文时我会格外小心。

      您希望单例 bean 会发生什么?如果一个对象引用了单例 bean,是否应该更新它?

      【讨论】:

      • 我不想重新加载 spring applicationcontext.xml 或任何已加载的 spring bean。我将尝试 Phill Sacre 发布的解决方案。
      【解决方案4】:

      我使用 JRebel 进行开发,我会非常警惕期望它会刷新您的配置。可以在 Java 上正常工作,但不能在 Spring 上工作。

      【讨论】:

        【解决方案5】:

        如果您想添加上下文,我已通过以下方式完成:

        public class ApplicationContextUtil
        {
           static String[] configFiles = {"applicationContextParent.xml"};
        
           private static ApplicationContext context = null;
        
           static
           {
               context = new ClassPathXmlApplicationContext ( configFiles );
           }
        
           public static void addContext( String[] newConfigFiles )
           {
               // add the new context to the previous context
               ApplicationContext newContext =  new ClassPathXmlApplicationContext ( newConfigFiles, context );
               context = newContext;
           }   
           public static ApplicationContext getApplicationContext ()
           {
               // return the context
               return context;
           }
        }
        

        这是您的上下文提供程序类。详情可以看my blog

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-29
          • 1970-01-01
          • 2021-10-20
          • 2021-06-03
          • 2018-12-15
          • 1970-01-01
          • 1970-01-01
          • 2014-11-16
          相关资源
          最近更新 更多