Lazy loading vs. pre-loading beans with Spring Framework - Digizol6

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Tuesday, September 9, 2008

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

10 comments:

  1. Thanks for describing the difference in using two factories

    ReplyDelete
  2. Short, but explains all.

    Thanks
    Michel

    ReplyDelete
  3. Simple and Clear explanation

    ReplyDelete
  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

    ReplyDelete
  5. Nice explanation of the difference. Thank you.

    ReplyDelete
  6. Exactly what I was looking for. Thanks a lot!

    ReplyDelete
  7. Simple and easily understandable explanation. keep it up.

    ReplyDelete
  8. Thanks For describing Lazy and Eager loading in Spring.
    Could you explain Singleton behavior for BeanFactor and ApplicationContext

    ReplyDelete
  9. 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.

    ReplyDelete

Post Top Ad

Responsive Ads Here