title: How does one programmatically create a Spring context?
body: <p>Does anyone know if there any way that I can programmatically create a bean context?</p>
  
  <p>I want to be able to do something like:</p>
  
  <pre><code>ConfigurableApplicationContext c = new ConfigurableApplicationContext();
  BeanDefinition bd = new BeanDefinition();
  bd.setId("id");
  bd.setClassName("classname");
  bd.setProperty("propertyName", propertyValue");
  ...etc...
  </code></pre>
  
  <p>or better still be able to inject a ready made bean into the application context:</p>
  
  <pre><code>c.addBean("beanId", beanObject);
  </code></pre>
  
  <p>Or if I'm using annotations:</p>
  
  <pre><code>c.setAnnotationAware(true);
  c.setAnnotationScanBasePackage("packagename");
  </code></pre>
  
  <p>or </p>
  
  <pre><code>c.addAnnotatedSpringClass("classnamethatisannotated");
  </code></pre>
  
  <p>The rationale for this is that I want to be able override bean definitions for the purpose of testing - In my test I create this new application context, configured with code in the test (not in xml) and then make this test application context have as a parent the SUT application context.</p>
  
  <p>I haven't found any code in the spring libraries that can do this. Has anyone built something like this? Would it be possible to build something like this? I know the former approach is doable, I'm not 100% sure the latter approaches will work without conditions.</p>