The way of loading Spring Beans is one of the most important difference between BeanFactory and ApplicationContext. The BeanFactory by default lazy loads the beans, it creates the bean only when the getBean() method is called. whereas ApplicationContext preloads all the singleton beans upon start-up.
We have two simple beans (Bean1 & Bean2), each having a no argument constructor.
Bean1.java
package com.javainterviewpoint; public class Bean1 { public Bean1() { System.out.println("Creating bean bean1"); } }
Bean2.java
package com.javainterviewpoint; public class Bean2 { public Bean2() { System.out.println("Creating bean bean2"); } }
SpringConfig.xml
<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-3.0.xsd"> <bean id="bean1" class="com.javainterviewpoint.Bean1"></bean> <bean id="bean2" class="com.javainterviewpoint.Bean2"></bean> </beans>
we have defined our two beans in the SpringConfig.xml
ClientLogic.java
With BeanFactory
package com.javainterviewpoint; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); } }
Here we have read our configurations with a BeanFactory, upon running. When we look into the console, we could see that nothing happens just loading up of xml bean definition.
Apr 16, 2015 4:58:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringConfig.xml]
Only when we add the below code, the beans will be called.
public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); //Get the bean1 instance Bean1 bean1=(Bean1)bf.getBean("bean1"); //Get the bean2 instance Bean2 bean2=(Bean2)bf.getBean("bean2"); } }
At console it will be
Apr 16, 2015 4:58:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringConfig.xml] Creating bean bean1 Creating bean bean2
With ApplicationContext
package com.javainterviewpoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ClientLogic { public static void main(String args[]) { ApplicationContext appContext = new ClassPathXmlApplicationContext("SpringConfig.xml"); } }
Upon running itself bean will be getting loaded. Even no need to call.
Apr 16, 2015 5:08:36 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3a1ec6: startup date [Thu Apr 16 17:08:36 IST 2015]; root of context hierarchy Apr 16, 2015 5:08:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringConfig.xml] Apr 16, 2015 5:08:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10e6817: defining beans [bean1,bean2]; root of factory hierarchy Creating bean bean1 Creating bean bean2
In order to lazy initialize the bean loading we will use “lazy-init” attribute set to true, so that bean will get loaded only when called.
<bean lazy-init="true" id="bean1" class="com.javainterviewpoint.Bean1"></bean> <bean lazy-init="true" id="bean2" class="com.javainterviewpoint.Bean2"></bean>
Once we run our Client Logic class beans will not get loaded unless and until called explicitly, as you can see the constructor is not called.
Apr 16, 2015 5:16:25 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3a1ec6: startup date [Thu Apr 16 17:16:25 IST 2015]; root of context hierarchy Apr 16, 2015 5:16:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringConfig.xml] Apr 16, 2015 5:16:25 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e01885: defining beans [bean1,bean2,bean3]; root of factory hierarchy
Note : One more point to be noted is that ApplicationContext will pre-load Singleton scoped beans only.
Leave a Reply