Ad

Friday, 27 March 2015

Lazy initialization of spring beans using lazy-init and default-lazy-init

In this post we will learn about the lazy initialization of spring beans using attribute lazy-init and default-lazy-init.By default spring container will create all the singleton bean at  container startup i.e when the spring container is loaded.This is necessary since if there is any error in creating the beans or setting the dependencies will be known to us at the container load time itself.But sometimes we don't need this behavior.We would like to create the bean only when it is requested and not at the container startup,thus reducing the container startup time.
 
Below code example will show how to lazy initialize the bean.

Employee- Spring bean which we will lazy initialize.
LazyInitDemo -  Main class which load the container and initializes the bean.
Studentcontext.xml - metadata configuration details for the spring container.

Employee.java

package bean;

public class Employee {

private String empId;

private String empName;

public Employee(String empId, String empName) {
this.empId = empId;
this.empName = empName;
System.out.println("Employee bean Created empId:" + empId + " empName:"
+ empName);
}

public String getEmpId() {
return empId;
}

public String getEmpName() {
return empName;
}

}

employeecontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="employee" class="bean.Employee" lazy-init="true">
<constructor-arg  name="empId" value="E01"/>
<constructor-arg  name="empName" value="Rahul"/>
</bean>

</beans>

As we can see in the application context file,we set the attribute lazy-init to true in the bean definition.This will make this bean to be create only when it is requested and not at the container load time.

LazyInitDemo.java

package bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LazyInitDemo {

public static void main(String[] args) {
ApplicationContext bf = new ClassPathXmlApplicationContext(
new String[] { "employeecontext.xml" });
System.out.println("Spring Container Laoded::");
System.out
.println("Going to request the employee bean from the container::");
Employee emp = bf.getBean("employee", Employee.class);
}
}

OUTPUT:

When we run LazyInitDemo class, we will get the below output

Spring Container Loaded::
Going to request the employee bean from the container::
Employee bean Created empId:E01 empName:Rahul


As we can see in the output,employee bean was not created at container loaded time and was created only when it was requested.

when we need all the beans defined in the application context to be lazy initialized,use the attribute default-lazy-init in the parent "beans" tag as shown 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-lazy-init="true">

<!--- bean 1 definition  -->

<!--- bean 2 definition  -->

</beans>

No comments:

Post a Comment