title: Best way to achieve @Autowired @Lazy @Components via annotations?
body: <p>Is there a way to have <code>@Lazy</code> loading <code>@Component</code>s that are still <code>@Autowired</code> in a factory via annotations? The problem I found is that by autowiring my lazy components in the factory, they will all be instantiated immediately once the factory is loaded, negating the lazy annotation.</p>
  
  <p>I have defined several lazy beans such as</p>
  
  <pre><code>@Component
  @Lazy
  public final class CloseableChromeWebDriver
        extends ChromeDriver
        implements DisposableBean {
  ...
  }
  
  @Component
  @Lazy
  public final class CloseableFirefoxWebDriver
        extends FirefoxDriver
        implements DisposableBean {
  ...
  }
  </code></pre>
  
  <p>It is important that they be lazy because</p>
  
  <ul>
  <li>Whenever one of them is created, the browser window pops up.</li>
  <li>my data driven tests may or may not require any one or all of them i.e. one run may be all Firefox, or may require Firefox and Chrome.</li>
  <li>this is more important because in fact I have six such beans - Firefox, Chrome, IE, Remote Firefox, Remote Chrome, Remote IE.</li>
  <li>So, if my tests only use one of them, then I only want that one browser to show, not all them.</li>
  </ul>
  
  <p>I have a Factory for getting the requested browser, but my first attempt at this failed because whenever any class used the factory, all of the autowired beans were being instantiated straight away, irrespective of whether they were actually requested. I understand this is because once the class is instantiated, it must instantiate all of the instance variables belonging to it.</p>
  
  <pre><code>@Component
  public final class WebDriverFactory {
     @Autowired
     private CloseableChromeWebDriver chromeWebDriver;
     @Autowired
     private CloseableFirefoxWebDriver firefoxWebDriver;
  
     public synchronized WebDriver getWebDriver(final Browser browser) {
       // get whatever webdriver is matched by the enum Browser.
     }
  }
  </code></pre>
  
  <p>So here is my second approach which is to ensure lazy loading by requesting the bean through the application context:</p>
  
  <pre><code>@Component
  public final class WebDriverFactory {
     private CloseableChromeWebDriver chromeWebDriver;
     private CloseableFirefoxWebDriver firefoxWebDriver;
     @Autowired
     private ApplicationContext appContext;
  
     public synchronized WebDriver getWebDriver(final Browser browser) {
        WebDriver driver = null;
        switch (browser) {
           case FIREFOX:
              if (firefoxRemoteWebDriver == null) {
                 firefoxRemoteWebDriver = appContext.getBean("closeableRemoteFirefoxWebDriver",
                       CloseableRemoteFirefoxWebDriver.class);
              }
              driver = firefoxRemoteWebDriver;
              break;
        // etc...
        return driver;
     }
  }
  </code></pre>
  
  <p>This approach achieves my goal, but I feel it really negates the usefulness of using annotations. Is there a purely annotation based way to achieve this?</p>
  
  <ul>
  <li>JDK 6</li>
  <li>Spring 3.2.6.RELEASE</li>
  <li>No xml, just annotations.</li>
  </ul>