Lazy Initiation in Spring IOC
As we know Spring’s bean factory is pre-initiate all the beans when first time creating the
facory. This is good practise because if there is any dependency error everything can be
resolved at the time of startup. This is not case in all the application scnarios.
The following are the few drawbacks in pre-initiating all the beans at startup:
- Takes long time to start the application. Since BeanFactory has to initiate all the beans even if it is
unnecessary. - More memory for storing all the beans.
Based on the above impacts, some applications required beans to initiate only when they are required.Spring provides
an attribute called lazy-init to inform the Spring IOC container for not creatinmg that bean at the startup.
lazy-init will be set as true to indicate the container. The beans will be created only when requested.
Conside when there is a bean which is initiated at the startup. But, it has depends-on attribute pointing to the bean which is set as
lazy-init=”true”. This case the can will be initiated without considering lazy-init value.
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package javabeat.net.spring.ioc;
/** * Source : http://www.javabeat.net */
public class Employee {
private String name;
private String empId;
private Address address;
public Employee() {
System.out.println("Employee Constructor");
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} |
Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package javabeat.net.spring.ioc;
/** * Source : http://www.javabeat.net */
public class Address {
public Address() {
System.out.println("Address Constructor");
}
private String street;
private String city;
private String pincode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
} |
LazyInit.java
1 2 3 4 5 6 7 8 9 10 11 12 | package javabeat.net.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** * source : www.javabeat.net */
public class LazyInit {
public static void main(String args[]) {
//Initializing context
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[]{"applicationContext.xml"});
System.out.println("After initialization");
applicationContext.getBean("addressBean");
}
} |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 | <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="employeeBean" class="javabeat.net.spring.ioc.Employee" /> <bean id="addressBean" class="javabeat.net.spring.ioc.Address" lazy-init="true" /> </beans> |






July 21, 2008
Spring Framework