Spring default behavior for lazy-init
Asked Answered
V

5

59

I am beginner to spring, ESP Inversion of control. I was puzzled understanding the difference between the following

 <bean id="demo" class="Demo" lazy-init="false"/>
 <bean id="demo" class="Demo" lazy-init="true"/>
 <bean id="demo" class="Demo" lazy-init="default"/>

To my understanding : lazy-init=false creates the bean at the startup and lazy-init=true doesn't create a bean at the startup rather creates the bean upon request for a particular bean. Correct me here, If my interpretation is wrong.

what exactly the default behavior of lazy-init is? How would it instantiate?

Vanderpool answered 26/2, 2013 at 15:26 Comment(0)
P
74

The default behaviour is false:

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

I suggest reading up

Prolate answered 26/2, 2013 at 15:30 Comment(4)
GWT4Ever: If the default behavior is false, why would one has to specify lazy-init="false" despite they can use lazy-init="default" right? Why two seperate values false, default if their behavior is same?Vanderpool
This is answered by Zagyi, it's all in the link too.Prolate
updated documentation is available hereResidue
I faced this issue due to self injection (needed for spring retry as the method being retried is not the one called by others). @ SpyBean obviously fails if self injection fields are not marked @ Lazy.Disini
C
26

For those coming here and are using Java config you can set the Bean to lazy-init using annotations like this:

In the configuration class:

@Configuration
// @Lazy - For all Beans to load lazily
public class AppConf {

    @Bean
    @Lazy
    public Demo demo() {
        return new Demo();
    }
}

For component scanning and auto-wiring:

@Component
@Lazy
public class Demo {
    ....
    ....
}

@Component
public class B {

    @Autowired
    @Lazy // If this is not here, Demo will still get eagerly instantiated to satisfy this request.
    private Demo demo;

    .......
 }
Cayuga answered 2/2, 2016 at 5:21 Comment(0)
D
22

The lazy-init="default" setting on a bean only refers to what is set by the default-lazy-init attribute of the enclosing beans element. The implicit default value of default-lazy-init is false.

If there is no lazy-init attribute specified on a bean, it's always eagerly instantiated.

Dara answered 26/2, 2013 at 15:29 Comment(0)
H
3

lazy-init is the attribute of bean. The values of lazy-init can be true and false. If lazy-init is true, then that bean will be initialized when a request is made to bean. This bean will not be initialized when the spring container is initialized and if lazy-init is false then the bean will be initialized with the spring container initialization.

Heeheebiejeebies answered 19/8, 2015 at 8:49 Comment(1)
One can set this for all beans in a file: <beans xmlns="springframework.org/schema/beans" ... default-lazy-init="true" >Motorbike
I
2

When we use lazy-init="default" as an attribute in element, the container picks up the value specified by default-lazy-init="true|false" attribute of element and uses it as lazy-init="true|false".

If default-lazy-init attribute is not present in element than lazy-init="default" in element will behave as if lazy-init-"false".

Imparisyllabic answered 13/7, 2016 at 13:31 Comment(1)
One can set this for all beans in a file: <beans xmlns="springframework.org/schema/beans" ... default-lazy-init="true" >Motorbike

© 2022 - 2024 — McMap. All rights reserved.