Categories
Spring Spring Core

Lazy Initialization

The Term Lazy initialization means to initialize the Object When needed or when requested. 

Spring IoC Container will create and initialize all the Singleton Beans while Creating the IoC Container itself. 

If we don’t want to initialize our Bean at the time of Container initialization, we can use Lazy-Init concept in Spring. 

Key Points

  • IoC Containers will become light weight and load faster.
  • Creating Bean only when needed. 
  • If there is any Configuration exception that will throw while we request the Bean at run time.
  • Without Lazy-Init the exception will be thrown while application startup itself. It will avoid application crashes while running. 
  • If the Lazy-Init Bean is dependent on a Eager Init Bean, it will not be considered as Lazy and initialized with other Singleton scoped beans. 
  • Lazy-Init should be enabled if the Bean works independently and has no dependency with other Beans.

Example Of Eager-Init Bean with Exception

Configuration Meta-Data

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="employeeController" class="net.geekcoders.controller.EmployeeController" lazy-init="default"></bean>
        <bean id="employeeService" class="net.geekcoders.services.EmployeeServiceImpl" lazy-init="default"></bean>
</beans>

I added the above Configuration with the wrong package name. Hence IoC Container will throw a ClassNotFoundexception while instantiating the Container Itself.

See console output below.

We shall try the same exception with Lazy-Init enabled.

Example of Lazy-Init Bean with Exception

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="employeeController" class="net.geekcoders.controller.EmployeeController" lazy-init="true"></bean>
        <bean id="employeeService" class="net.geekcoders.services.EmployeeServiceImpl" lazy-init="true"></bean>
</beans>

Now the above Bean Definition is Lazy-Init enabled for both the Beans. If you run the application, 

  • IoC Container will get Instantiated successfully.
  • Upon calling EmployeeController Bean the ClassNotFound exception will be thrown.

  • As you see the Logs for Lazy-Init, the Container gets initialized first and then initialized the Beans when we call for the first time.
  • We should be careful on using Lazy-Init because the validation of the Bean occurs later while running the actual application. In case of any issues the exception will be thrown at runtime.

Example of Lazy-Init Bean with Singleton Scoped Bean

As I Stated earlier, If you use Lazy-Init beans as a Dependency to a Singleton scoped Bean, it will not be considered as Lazy and IoC Container will create an Instance as like other Beans while Startup.

Configuration Meta-Data

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="employeeController" class="net.geekcoders.controllers.EmployeeController">
        	<property name="employeeService" ref="employeeService"></property>
        </bean>
        <bean id="employeeService" class="net.geekcoders.services.EmployeeServiceImpl" lazy-init="true"></bean>
</beans>

As per the above configuration, 

  • employeeController Bean is not enabled as Lazy and has employeeService Bean as Dependency in it. 
  •  The noted point here is the Dependent employeeService Bean is Lazy-Init enabled.

While running the application you can see the below logs in Console.

Here, EmployeeController and EmployeeServiceImpl Beans have been created and Initialized while IoC Container is being Created. And the employeeService bean is not considered as Lazy because it is Dependent to a Singleton scoped Bean.

Example of Lazy-Init

Now in this part we provide Both the EmployeeController and EmployeeService as Lazy-Init true as below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="employeeController" class="net.geekcoders.controllers.EmployeeController" lazy-init="true">
        	<property name="employeeService" ref="employeeService"></property>
        </bean>
        <bean id="employeeService" class="net.geekcoders.services.EmployeeServiceImpl" lazy-init="true"></bean>
</beans>

Now IoC container won’t initialize the EmployeeController and EmployeeService Beans while Container Creation itself creates and Initialize it only when requested. 

When running the application the logs will now be like below.

As per the above logs,

  • IoC Container has created and initialized first
  • Upon request, EmployeeController and its Dependency EmployeeService Beans have been created and Initialized.

That’s all for now on Lazy-Init Spring with Example Article.

Happy Reading!!! 

2 replies on “Lazy Initialization”

Leave a Reply

Your email address will not be published. Required fields are marked *