5

How to lazy init a dependency that is @Inject?

public class ClassA {
  @Inject
  ClassB classB;
}


@Configuration
public class Config {
    @Bean
    public ClassA classA() {
        return new ClassA();
    }

    @Bean
    @Lazy
    public ClassB classB() {
        return new ClassB();
    }
} 

When classA bean is instantiated, classB bean is also instantiated, despite of @Lazy annotation. How can I avoid classB bean instantiation ?

Ana
  • 312
  • 2
  • 9

1 Answers1

1

You can't really do it like that. Like Sotirios said, Spring needs to instantiate it to inject it into ClassA. You probably can do it manually with the application context. Something like :

public class ClassA {

    @Inject
    private ApplicationContext appContext;

    private ClassB classB;

    //Bean will be instanciated when this method is called
    public ClassB getClassB() { 
        if (classB == null) {
            classB = appContext.getBean(ClassB.class);
        }
        return classB;
    }
}

And then use the getter to access the object.

Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
  • 1
    So lazy init is only for properties that are not '@Inject' or '@Autowired' ? This seems a bit restrictive because, as far as I know, these annotations are always used in new versions of Spring. If the bean was set in an xml file, the lazy would work? – Ana Jul 11 '13 at 09:24
  • I think you don't understand how Lazy init, bean scope and injection works together. Lazy init is meant to be used with '@Inject' or '@Autowired', but it don't really make sense to inject a lazy bean in a bean that has a singleton scope. If you put the bean in an XML it would do the same thing... At the end, Singleton bean with Lazy = true are instantiated like a Prototype bean, the difference is that when the bean is instantiate it always returns the same instance. – Jean-Philippe Bond Jul 11 '13 at 11:35
  • Thank you very much for your answer. Indeed, I don't understand the relation between lazy init and bean scope. So lazy in singleton doesn't make sense because spring will make an instance of the bean on startup; Prototype beans are not initalized at startup, but isn't this by default? Where does @Lazy make the difference? Or lazy dependencies of a prototype bean are not initialized, even when the prototype bean is initialized, but lazy dependencies of singleton bean are always initialized? – Ana Jul 11 '13 at 14:00
  • What I mean is that it don't really make sense to inject a lazy singleton bean into a bean that has a singleton scope but is not lazy. Lazy beans are generally singleton. For example, if you inject a lazy singleton bean in a prototype bean, the singleton bean will not be instantiated until the prototype bean is instantiated. Lazy mean something in this case, if your singleton was not lazy it would be instanciated on startup. So it started to make sense when you inject a lazy singleton bean into a prototype bean that is not instantiated on startup. – Jean-Philippe Bond Jul 11 '13 at 17:37