Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

"http://www.w3.org/TR/html4/loose.dtd">









Lazily-instantiated beans


The default behavior for ApplicationContext implementations is to create and configure all of
its singleton beans as part of its initialization process. This helps the user to find out all the
errors in the configuration or in the surrounding environment during the application
startup. But, there are times when this behavior is not what is wanted and user want to create the bean whenever it is required which is generally called as the lazy
initialization of the beans.


When configuring beans via XML, this lazy loading is controlled by the 'lazy-init' attribute on the <bean/>element; It is also possible to control lazy-initialization at the container level by using the 'default-lazy-init'
attribute on the <beans/> element.


Example:-




<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">

   
<bean id="test" class="com.visualbuilder.beans.TestBean" lazy-init="true" />

   
<bean id="disposableBean" class="com.visualbuilder.beans.DisposableInitBean" scope="singleton" />

   
<bean id="collectionExample" class="com.visualbuilder.beans.CollectionInjectionBean" scope="singleton" lazy-init="true">

       
<property name="products">

           
<list>

               
<value>CAR</value>

               
<value>MOTORCYCLE</value>

               
<value>SCOOTER</value>

           
</list>

       
</property>

       
<property name="productSet">

           
<set>

               
<value>CAR</value>

               
<value>MOTORCYCLE</value>

               
<value>SCOOTER</value>

           
</set>

       
</property>


        <property name="prices">

           
<map>

               
<entry key="
CAR" value="100" />

               
<entry key="
MOTORCYCLE" value="200" />

               
<entry key="
SCOOTER" value="10" />

           
</map>

       
</property>

   
</bean>  

   
<bean id="
typeCollectionExample" class="com.visualbuilder.beans.TypeCollectionInjectionBean" scope="singleton" lazy-init="true">

       
<property name="
products">

           
<list>

               
<value>
CAR</value>

               
<value>
MOTORCYCLE</value>

               
<value>
SCOOTER</value>

           
</list>

       
</property>

       
<property name="prices">

           
<map>

               
<entry key="CAR" value="100" />

               
<entry key="MOTORCYCLE" value="200" />

               
<entry key="SCOOTER" value="10" />

           
</map>

       
</property>

   
</bean>

   
<bean id="
constructorInjection" class="com.visualbuilder.beans.ConstructorInjectionBean" scope="singleton" lazy-init="true">

       
<constructor-arg type="
java.lang.String" value="John Smith" />

       
<constructor-arg index="
1" value="25" />

       
<constructor-arg value="
M" />

   
</bean>


    <bean id="setterInjection" class="com.visualbuilder.beans.SetterInjectionBean" sscope="singleton" lazy-init="true">

       
<property name="
name" value="John Smith" />

       
<property name="
age" value="25" />

       
<property name="
sex" value="M" />

   
</bean>

   
<bean id="
wrapperBean" class="com.visualbuilder.beans.WrapperBean" scope="singleton" lazy-init="true">

       
<property name="
collectionInjectionBean">

           
<ref bean="
collectionExample" />

       
</property>

       
<property name="
constructorInjectionBean">

           
<ref bean="
constructorInjection" />

       
</property>

       
<property name="
disposableInitBean">

           
<ref bean="
disposableBean" />

       
</property>

       
<property name="
setterInjectionBean">

           
<ref bean="
setterInjection" />

       
</property>

       
<property name="
typeCollectionInjectionBean">

           
<ref bean="
typeCollectionExample" />

       
</property>

   
</bean>

</beans>



ApplicationContextExample.java




package com.visualbuilder.factory;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.TestBean;


public class ApplicationContextExample {

    public static void main (String [] ar){

               
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/visualbuilder/xml/spring-beans.xml"});

               
TestBean bean = (TestBean)context.getBean("test");

        System.out.println(bean.toString());

       
}

}


Output:-


The below output will come when lazy-init="false" is set in spring-beans.xml file. The output shows that Spring created all the singleton beans intially.


Jun 2, 2008 12:38:25 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Mon Jun 02 12:38:25 IST 2008]; root of context hierarchy

Jun 2, 2008 12:38:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Jun 2, 2008 12:38:26 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@d6c16c

afterPropertiesSet method is called

Jun 2, 2008 12:38:26 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@d6c16c: defining beans [test,disposableBean,collectionExample,typeCollectionExample,constructorInjection,setterInjection,wrapperBean]; root of factory hierarchy

Bean is created

Bean has been called



If the lazy intialization is true then singleton beans are not created by Spring. Hence belwo example will be produced by same program.


Jun 2, 2008 12:38:25 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Mon Jun 02 12:38:25 IST 2008]; root of context hierarchy

Jun 2, 2008 12:38:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Jun 2, 2008 12:38:26 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@d6c16c

Jun 2, 2008 12:38:26 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@d6c16c: defining beans [test,disposableBean,collectionExample,typeCollectionExample,constructorInjection,setterInjection,wrapperBean]; root of factory hierarchy








                    

Copyright © 2012 VisualBuilder. All rights reserved