title: BeanFacotry and Bean of same type
body: <p>I have two services that implements my service interface, and I want to select the right service based on some rule at runtime, for example:</p>
  
  <pre><code>public interface Service { public String doSomething(String param); }
  
  @Component("serviceAimpl") 
  public class ServiceAImpl implements Service{ 
      public String doSomething(String param){ 
          return param + "A";
      }
  }
  
  @Component("serviceBimpl") 
  public class ServiceBImpl implements Service{ 
      public String doSomething(String param){
          return param;
      }
  }
  </code></pre>
  
  <p>The ServiceAImpl must be used when param starts with "1" and the ServiceBImpl must be used otherwise, is there any way to do something like the above:</p>
  
  <pre><code>@Component public class ServiceImpl implements Service {
      @Autowired private Map&lt;String, Service&gt; services;
      public String doSomething(String param) {
           if(param.startsWith("1")
               return services.get("serviceAimpl").doSomething(param);
           else
               return services.get("serviceBimpl").doSomething(param);
      }
  }
  </code></pre>
  
  <p>This way the factory is of the same type as the beans itself and so there is no need to create a new type to get the bean and then use the bean. Is that valid in spring or not? Is there any approach or convention to do something like the described above (ignore the fact that Im only concatenating Strings there) ?</p>