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 ?

1
  • 1
    It's lazy until you need it. Spring needs to instantiate it to inject it into ClassA, so it gets initialized at that point. Commented Jul 10, 2013 at 20:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.