title: Spring context dynamic change
body: <p>I've read that dynamic bean definition change. I try it in a simple code example (see code below), and I find it very attractive in situations where I don't want to stop server but add/change bean definition.</p>
  
  <p><em><strong>Questions:</em></strong> </p>
  
  <ol>
  <li><em>Is it safe do to so (see code below)?</em></li>
  <li><p><em>I've read that it is possible to achieve bean definition change in runtime with help of <code>StaticApplicationContex</code> or <code>BeanPostProcessor</code> or <code>BeanFactoryPostProcessor</code>? So what is the difference?</em></p>
  
  <pre><code>public class Main {
  final static String header = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n" +
          "&lt;beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
          "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
          "       xmlns:context=\"http://www.springframework.org/schema/context\"\n" +
          "       xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd\"&gt;\n" +
          "    &lt;context:annotation-config /&gt;\n" +
          "    &lt;context:component-scan base-package=\"vbah\"/&gt;";
  
  final static String contextA =
          "&lt;bean id=\"test\" class=\"java.lang.String\"&gt;\n" +
                  "\t\t&lt;constructor-arg value=\"fromContextA\"/&gt;\n" +
                  "&lt;/bean&gt;&lt;/beans&gt;";
  
  final static String contextB =
          "&lt;bean id=\"test\" class=\"java.lang.String\"&gt;\n" +
                  "\t\t&lt;constructor-arg value=\"fromContextB\"/&gt;\n" +
                  "&lt;/bean&gt;&lt;/beans&gt;";
  
  public static void main(String[] args) throws IOException {
      //create a single context file
      final File contextFile = new File("src/resources/spring-config.xml");
  
      //write the first context into it
      FileUtils.writeStringToFile(contextFile, header + contextA);
  
      //create a spring context
      FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
              new String[]{contextFile.getPath()}
      );
  
      //echo "fromContextA"
      System.out.println(context.getBean("test"));
  
      //write the second context into it
      FileUtils.writeStringToFile(contextFile, header + contextB);
  
      //refresh the context
      context.refresh();
  
      //echo "fromContextB"
      System.out.println(context.getBean("test"));
  }
  }
  </code></pre></li>
  </ol>
  
  <p><em><strong>EDIT:</em></strong></p>
  
  <p>Can you answer the questions below:</p>
  
  <ol>
  <li>As I understand <code>BeanPostProcess</code> allow you to modify already existed bean instances at runtime by wrapping the object with proxy. Am I right?</li>
  <li><p>AbstractApplicationContext#refresh() drop all singleton beans and recreate them. </p>
  
  <ul>
  <li>But If I want to change the definition of  prototype/custom scoped bean?</li>
  <li>If I've got two beans: A and B. A has reference to B. If I change the bean definition in such way that it doesn't contain definition of B. Than B instances will be destroyed, but new instances   won't be created. Than A will get a <code>null</code> dependency. Am I right?</li>
  </ul></li>
  <li><p><code>StaticApplicationContext</code> and <code>BeanFactoryPostProcessor</code> both allow me to change a bean definition in runtime. But what are the difference, pros/cons?</p></li>
  <li><strong>[Main question]</strong> Why Spring has 3 mechanism to achieve the same goal. Can you make a brief compoarison (or usecases examples) between <code>AbstractApplicationContext#refresh()</code>, <code>StaticApplicationContext</code> and <code>BeanFactoryPostProcessor</code> please.</li>
  </ol>