title: Implement a simple factory pattern with Spring 3 annotations
body: <p>I was wondering how I could implement the simple factory pattern with Spring 3 annotations.  I saw in the documentation that you can create beans that call the factory class and run a factory method.  I was wondering if this was possible using annotations only.</p>
  <p>I have a controller that currently calls</p>
  <pre><code>MyService myService = myServiceFactory.getMyService(test);
  result = myService.checkStatus();
  </code></pre>
  <p>MyService is an interface with one method called checkStatus().</p>
  <p>My factory class looks like this:</p>
  <pre><code>@Component
  public class MyServiceFactory {
  
      public static MyService getMyService(String service) {
          MyService myService;
          
          service = service.toLowerCase();
          
          if (service.equals(&quot;one&quot;)) {
              myService = new MyServiceOne();
          } else if (service.equals(&quot;two&quot;)) {
              myService = new MyServiceTwo();
          } else if (service.equals(&quot;three&quot;)) {
              myService = new MyServiceThree();
          } else {
              myService = new MyServiceDefault();
          }
          
          return myService;
      }
  }
  </code></pre>
  <p>MyServiceOne class looks like this :</p>
  <pre><code>@Autowired
  private LocationService locationService;
  
  public boolean checkStatus() {
        //do stuff
  }
  </code></pre>
  <p>When I run this code the locationService variable is always null.  I believe this is because I am creating the objects myself inside the factory and autowiring is not taking place.  Is there a way to add annotations to make this work correctly?</p>
  <p>Thanks</p>