Webcam Chat QuickBooks Advice international calling cards international phone cards
JavaBeat Java Books Certifications Certifications Kits Articles Tutorials Tips QNA Book Store Interview Questions SCJP 1.5 SCJP 1.6 SCWCD 5.0 SCBCD 5.0 SCEA SCJA Feeds

How to lazy initialize Spring beans?

Author : JavaBeat
Topic : spring
Date : Mon Jul 21st, 2008
Feedback Request New Tips Print Email

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




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


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


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




<?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>

Feedback Print Email


JavaBeat Website (2004-2011), India
javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2011), JavaBeat


Technology Blogs
Technology blogs Technology Blogs
blog log