Java News Tips Software
Java News Tips Software | Contact | Facebook | Twitter RSS

Lazy loading vs. pre-loading beans with Spring Framework

Spring LogoSpring framework can instantiate and bind (called loading) related Java objects (called beans) according to a given configuration. An XML file can easily be used to define these bindings. Spring framework supports two different types of loading methods; lazy loading and pre-loading respectively managed by BeanFactory and ApplicationContext containers.

Lazy Loading

A bean is loaded only when an instance of that Java class is requested by any other method or a class. org.springframework.beans.factory.BeanFactory (and subclasses) container loads beans lazily. Following code snippet demonstrate lazy loading, concentrate on how "beans.xml" spring configuration file is loaded by BeanFactory container class.

BeanFactory factory = new XmlBeanFactory(
new InputStreamResource(
new FileInputStream("beans.xml"))); // 1
Employee emp = (Employee) factory.getBean("employeeBean"); // 2

Even though "beans.xml" configuration file is loaded with BeanFactory container in line number 1, none of the beans will be instantiated. Instantiation takes place only at line number 2, where bean called "employeeBean" is requested from container. Since the class is instantiated at getBean() method call, time spend to return this method will vary depending on the instantiated object.

Pre-loading

All beans are instantiated as soon as the spring configuration is loaded by a container. org.springframework.context.ApplicationContext container follows pre-loading methodology.

ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml"); // 1
Employee emp = (Employee) context.getBean("employeeBean"); // 2

As all singleton beans are instantiated by container at line number 1, this line will take some considerable time to complete. However line number 2 will return the bean instance immediately since instances are already available inside the container.

Point to note

Decision to choose one from these two methods would depend solely on application specific requirements. Some applications need to load as soon as possible while many others would probably willing to spend more time at startup but serve client requests faster. However some of the beans defined in a configuration may only be used rarely, so instantiating such classes at start up would not be a wise decision. Similarly, some Java instances would be highly resource consuming; leading not to instantiate at start up.

Related articles
Better use <object> instead of <bean> in Spring

Labels: , ,


11 Comments

  1. Thanks for describing the difference in using two factories
  2. Short, but explains all.

    Thanks
    Michel
  3. Anonymous Anonymous on July 30, 2009 11:08 AM  
    Simple and Clear explanation
  4. Lazy instantiation should not be done using FileInputStream. With spring 2, using it the way you described, validation mode of the xml would not be automatically determined, and you would get BeanDefinitionStoreException. That approach should be avoided. Instead you should specify your bean to be lazy initialized by adding "lazy-init="true" to your bean declaration.

    Erni
  5. Nice explanation of the difference. Thank you.
  6. Anonymous Anonymous on April 25, 2011 10:25 PM  
    Exactly what I was looking for. Thanks a lot!
  7. Anonymous Anonymous on August 17, 2012 5:47 AM  
    Simple and easily understandable explanation. keep it up.
  8. Thanks For describing Lazy and Eager loading in Spring.
    Could you explain Singleton behavior for BeanFactor and ApplicationContext
  9. Thanks for the nice explanation.
  10. Anonymous Anonymous on January 06, 2016 9:41 AM  
    Article is good. One more thing you can add here, if you want to lazy load bean, you can use bean attribute "lazy-init".
    ex:

    ref: http://docs.spring.io/spring/docs/2.5.3/reference/beans.html#beans-factory-lazy-init
  11. Anonymous Anonymous on April 15, 2016 12:09 PM  
    if you want to get same eager behaviour with BeanFactory then You have to invoke the method 'preinstantiateSingletons()' on your XmlBeanFactory to get the same behaviour as an application context.
ABOUT AUTHOR
Page Views :
Email :
PREVIOUS ARTICLES
Select Month:
TOP
Free counter and web stats