Sunday, 25 February 2018

5.0 Scope and Life-Cycle of a BEAN

Scope of a BEAN

  1.  life cycle of a bean
  2.  How long does the bean live
  3.  How many instances are create
  4.  How is the bean shared


<bean id="anotherSwap" 
                   class="com.spring.demo.AnotherLogic">
</bean>

here the by-default scope is Singleton

  • Spring container creates only one instance
  • It is cached in memory
  • All request for the bean will return a shared reference to the same bean

Example - obj1 and obj2 pointing to the same bean

Swap obj1=(Swap) context.getBean("Dontswap", Swap.class);

DontSwap obj2=(DontSwap) context.getBean("Dontswap",DontSwap.class);
 
we can explicitly specify bean scope
<bean id="anotherSwap" 
                   class="com.spring.demo.AnotherLogic
                   scope="singleton">
</bean>




































LIFE CYCLE OF A BEAN

























---------------------------------------------------------------------------------
MyAppLifeCycleTest.java
---------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyAppLifeCycleTest {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("lifecycle-applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(1, 2);
context.close();

}

}



---------------------------------------------------------------------------------
lifecycle-applicationContext.xml
---------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" 
class="com.spring.demo.SecondMethodSwap"
init-method="doStuff"
destroy-method="doMoreStuff">
<!-- Constructor injection -->
<constructor-arg ref="anotherSwap" />
</bean>


</beans>



---------------------------------------------------------------------------------
SecondMethodSwap.java
---------------------------------------------------------------------------------
package com.spring.demo;

public class SecondMethodSwap implements Swap{

private Logic logic;
public SecondMethodSwap(Logic thelogic){
logic=thelogic;
}
public void doSwap(int a, int b) {
a=a+b;
b=a-b;
a=a-b;
System.out.println("Swap using 2 variable : a = "+a+" and b= "+b);
}

public void anotherLogic(int a, int b) {
logic.anotherLogic(a,b);
}
public void doStuff(){
System.out.println("SecondMethodSwap : inside doStuff");
}
public void doMoreStuff(){
System.out.println("SecondMethodSwap : inside doMoreStuff");
}

}


---------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------
Feb 25, 2018 4:27:44 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 16:27:44 IST 2018]; root of context hierarchy
Feb 25, 2018 4:27:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [lifecycle-applicationContext.xml]
SecondMethodSwap : inside doStuff
Swap using 2 variable : a = 2 and b= 1
Feb 25, 2018 4:27:45 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 16:27:44 IST 2018]; root of context hierarchy
SecondMethodSwap : inside doMoreStuff

No comments:

Post a Comment