Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring default behavior for lazy-init

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?

like image 688
srk Avatar asked Feb 26 '13 15:02

srk


People also ask

What is default lazy-init in spring?

By default in Spring, all the defined beans, and their dependencies, are created when the application context is created. In contrast, when we configure a bean with lazy initialization, the bean will only be created, and its dependencies injected, once they're needed.

What is the default behavior of spring ApplicationContext?

By default spring implementations of ApplicationContext eagerly instantiate all the singleton beans at startup. This helps us to ensure all the configuration and dependencies are intact. This default behaviour can be customized as below by just adding property lazy-init=”true”.

What is the default bean loading strategy of the spring container?

By default, Spring creates all singleton beans eagerly at the startup/bootstrapping of the application context. The reason behind this is simple: to avoid and detect all possible errors immediately rather than at runtime.


2 Answers

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

like image 59
Vjeetje Avatar answered Sep 24 '22 08:09

Vjeetje


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;      .......  } 
like image 39
DavidR Avatar answered Sep 25 '22 08:09

DavidR