title: How to generate a list of beans dynamically
body: <p>Hy</p>
  
  <p>I'm looking for a way to 'simplify'/shorten my spring configuration.
  I' ve got this Generic service that looks something like:</p>
  
  <pre><code>public class GenericService&lt;T&gt; {
     private Class&lt;T&gt; targetClass;
     public void setTargetClass(Class&lt;T&gt; targetClass) {this.targetClass = targetClass;}
     public void doSomething() {...}
  }
  </code></pre>
  
  <p>and in my spring-config.xml file I have</p>
  
  <pre><code>&lt;bean id="entity1Service" class="GenericService"&gt;
     &lt;property name="targetClass" value="model.Entity1" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entity2Service" class="GenericService"&gt;
     &lt;property name="targetClass" value="model.Entity2" /&gt;
  &lt;/bean&gt;
  
  ...
  </code></pre>
  
  <p>I'm trying to build a factory that will build all these services for me so that I could write something like this in my spring-config.xml</p>
  
  <pre><code>&lt;bean class="ServiceFactory"&gt;
     &lt;property name="targets"&gt;
        &lt;list&gt;
          &lt;value&gt;model.Entity1&lt;/value&gt;
          &lt;value&gt;model.Entity2&lt;/value&gt;
        &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>which would generate 2 beans (one named entity1Service, the other entity2Service). Get-it?</p>
  
  <p>How would I start? I've looked at BeanFactory (not to be confused with FactoryBean!) but fail to see how to hookup everything up.</p>
  
  <p>It would be even better if my factory could scan my packages and generate a service when it finds an entity (either through annotation or interface implementation), a little like @EnableJpaRepositories annotation in spring-data does for all JpaRepository interfaces.</p>
  
  <p>Thanks for any insights, examples, pointers...</p>
  
  <p>w.</p>