Best way to achieve @Autowired @Lazy @Components via annotations?

You have to have @Lazy annotation on your component as well at the point where it is @Autowired in. This is because if you don’t have @Lazy on your component, then it is eagerly created as a bean, and if you have don’t have an @Lazy on your @Autowired then again it is eagerly created and injected into the bean. So try the following and it should just work:

public final class WebDriverFactory {
   @Autowired @Lazy
   private CloseableChromeWebDriver chromeWebDriver;
   @Autowired @Lazy
   private CloseableFirefoxWebDriver firefoxWebDriver;

Leave a Comment