Search code examples
springspring-ioc

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?


Solution

  • 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