title: Registering beans(prototype) at runtime in Spring
body: <p>Just need something evaluated by the community. Following is a snippet of code, which is a simple factory that creates instances of a particular type. The method will register the bean in the context as a prototype and return the instance. This is the first time I am configuring beans at run time. Could you kindly evaluate and provide feedback? thank you in advance.</p>
  
  <pre><code>package au.com.flexcontacts.flexoperations;
  
  import org.springframework.beans.BeansException;
  import org.springframework.beans.factory.config.BeanDefinition;
  import org.springframework.beans.factory.config.ConstructorArgumentValues;
  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  import org.springframework.beans.factory.support.GenericBeanDefinition;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  import org.springframework.context.support.AbstractApplicationContext;
  
  import au.com.flexcontacts.exceptions.SyncClassCreactionError;
  
  /**
   * @author khushroo.mistry
   * Class purpose: Simple Factory to create an 
   * instance of SynchroniseContactsService and register it in the Spring IoC.
   */
  public final class FLEXSyncFactory implements ApplicationContextAware {
  
      private static AbstractApplicationContext context;
  
  
      /**
       * @param username
       * @param password
       * @param syncType
       * @return the correct service class
       * @throws SyncClassCreactionError
       * The method registers the classes dynamically into the Spring IoC
       */
      public final SynchroniseContactsService createSyncService(String username, String password, SyncType syncType) throws SyncClassCreactionError {
  
          DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
  
          try {
  
              //Register the bean in the IoC
              BeanDefinition bdb = new GenericBeanDefinition();
              bdb.setBeanClassName(syncType.getClassName());
              bdb.setScope("prototype");
              ConstructorArgumentValues constructor = bdb.getConstructorArgumentValues();
              constructor.addIndexedArgumentValue(0, username);
              constructor.addIndexedArgumentValue(1, password);
              beanFactory.registerBeanDefinition(syncType.getInstanceName(), bdb);
  
              //Return instance of bean
              return (SynchroniseContactsService) beanFactory.getBean(syncType.getInstanceName());
          } catch (Exception e) {
              e.printStackTrace();
              throw new SyncClassCreactionError("Error: Illegal Handler");
          }
  
      }
  
      public void setApplicationContext(ApplicationContext applicationContext)
      throws BeansException {
          context = (AbstractApplicationContext) applicationContext;
  
      }
  
  }
  </code></pre>
  
  <p>FLEX Sync factory has been configured in the IoC container as a singleton. So to create a new sync manager I do the following:</p>
  
  <pre><code>flexSyncFactory.createSyncService(userName, password, SyncType.FULL);
  </code></pre>
  
  <p>I am using Spring 3.1. Please review and provide your valuable feedback.</p>
  
  <p>kind regards.</p>