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"?> <property name="prices"> <bean id="setterInjection" class="com.visualbuilder.beans.SetterInjectionBean" sscope="singleton" lazy-init="true"> |
ApplicationContextExample.java
package com.visualbuilder.factory; import org.springframework.context.ApplicationContext; import com.visualbuilder.beans.TestBean; public class ApplicationContextExample { |
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
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin




