title: Add Bean Programmatically to Spring Web App Context
body: <p>Because of a plug-in architecture, I'm trying to add a bean programmatically to my webapp.  I have a Spring bean created through the <code>@Component</code> annotation, and i am implementing the <code>ApplicationContextAware</code> interface.</p>
  
  <p>My override function looks like this:</p>
  
  <pre><code>@Override
  public void setApplicationContext(ApplicationContext applicationContext)
          throws BeansException {
  
      // this fails
      this.applicationContext = (GenericWebApplicationContext) applicationContext;
   }
  </code></pre>
  
  <p>Basically, I can't figure out how to add a bean to the applicationContext object given to setApplicationContext.  Can anyone tell me how I am going about this the wrong way?</p>
  
  <p>Ok, this is what i ended up with as the solution:</p>
  
  <pre><code>@Override
  public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry bdr)
          throws BeansException {
      BeanDefinition definition = new RootBeanDefinition(
              &lt;My Class&gt;.class);
  
      bdr.registerBeanDefinition("&lt;my id&gt;", definition);
  }
  </code></pre>