title: AnnotationConfigApplicationContext register & refresh another configuration class
body: <p>I'm pre initializing an AnnotationConfigApplicationContext (register + refresh) inside a service locator (Singleton) with a configuration class (mock). </p>
  
  <p>When I start my application I set a configuration class property. I would like to replace the configuration class already registered and reload my configuration.</p>
  
  <p>As "refresh" method can be uses once, is it possible or may I set directly at init the correct configuration class ? </p>
  
  <p>Config class 1</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = { "com.xxx.core.service.ws.controller" })
  @EnableWebMvc
  public class ContextCoreMockConfiguration {
  }
  </code></pre>
  
  <p>Config class 2</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @ImportResource(value={ "classpath:sessionFactory-datasource-spring.xml"} )
  @ComponentScan(basePackages = { "com.xxx.core.service.impl"
                                , "com.xxx.core.dao.impl" })
  public class ContextCoreServiceConfiguration {
  }
  </code></pre>
  
  <p>Singleton initializing context</p>
  
  <pre><code>public class AnnotationContextCoreLocator { 
      private AnnotationConfigApplicationContext  context; // Context IOC
  
      private AnnotationContextCoreLocator() {
          this.context = new AnnotationConfigApplicationContext();
          this.context.register(ContextCoreMockConfiguration.class);
          this.context.refresh();
      }
  
  
      /**
       * Context
       * @return AnnotationConfigApplicationContext
       */
      public static AnnotationConfigApplicationContext  getInstance() {
          return SingletonHolder.INSTANCE.getContext();
      }
  
      /**
       * singleton instanciation
       * @author asi
       *
       */
      private static class SingletonHolder {
          static final AnnotationContextCoreLocator INSTANCE = new AnnotationContextCoreLocator();
      }
  
      private AnnotationConfigApplicationContext  getContext() {
          return context;
      }
  }
  </code></pre>
  
  <p>The idea is to be able at runtime to change configuration class of Singleton</p>
  
  <pre><code>String configCoreClass = servlet.getInitParameter("contextCoreConfigurationClass");
  AnnotationContextCoreLocator.getInstance().register( Class.forName(configCoreClass) );
  AnnotationContextCoreLocator.getInstance().refresh();
  </code></pre>
  
  <p>Problem is that "refresh" method can be used once. I could remove "refresh" from my Singleton and do it at 1st use but I would like to change injection at runtime.</p>