title: How can I generate/create new spring beans at runtime with a bean definition object?
body: <p>I am new to Spring, and I would like to write a beanGenerator for a template bean. I would like to use this generator to overcome thread-safe concerns. Can anyone help me to add / modify the code to make this work? It's kind of hard to describe my real issue, so I abstract the issue in the following code:</p>
  
  <pre><code>abstract class BeanDefinition {
  
    abstract public void preprocess();
  
  }
  
  // now we have 1st user specific bean :
  class UserSpecifiedBeanDefinition extends BeanDefinition{
  
    @override
    public void preprocess() {
       // do something
    }
  
  }
  
  
  // we could have more user-specific beans that extend BeanDefinition
  ....
  
  
  
  
  // Following generator class is used to generate beans
  
  public class BeanGenerator {
  
    private BeanDefinition beanDefinition;
    public BeanGenerator(BeanDefinition beanDefinition) {
      this.beanDefinition = beanDefinition;
    }
    public generate() {
      BeanDefinition newBean = // create new bean based on beanDefinition? how can I make this work??
      newBean.preprocess();
      return newBean;
    }
  }
  
  // In spring.xml, I would like to use them like:
  &lt;bean id="generator1" class="com.xxx.xxx.BeanGenerator"&gt;
      &lt;constructor-arg name="beanDefinition" ref="userSpecifiedBeanDefinition"/&gt;
  &lt;/bean&gt;
  </code></pre>