Saturday, 25 August 2012

Spring Bean scopes with examples

This is 6 of 13 parts of tutorial series

Tutorial Content:

Part-1:Introduction to spring framework
Part-2:Dependency injection(ioc) in spring
Part-3:Spring hello world example in eclipse
Part-4:Dependency injection via setter method in spring
Part-5:Dependency injection via constructor in spring
Part-6:Spring Bean scopes with examples
Part-7:Initializing collections in spring
Part-8:Beans Autowiring in spring 
Part-9:Inheritance in Spring 
Part-10:Spring ApplicationContext 
Part-11:Spring lifetime callbacks 
Part-12:BeanPostProcessors in Spring 
Part-13:Annotation based Configuration in spring 

In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller.
There are 5 types of bean scopes supported in spring
  1. singleton – Scopes a single bean definition to a single object instance per Spring IoC container.
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request.
  4. session – Return a single bean instance per HTTP session.
  5. globalSession – Return a single bean instance per global HTTP session. 
In many cases,spring's core scopes i.e.singleton and prototype are used.By default scope of beans is singleton.
Here we will see singleton and prototype scopes in more details.

Singleton bean scope:

Example:

For configuring spring in your eclipse ide please refer  hello world example

1.Country.java:

This is simple pojo class having some attributes so here country has name.
Create Country.java under package org.arpit.javapostsforlearning.Copy following content into Country.java.

package org.arpit.javapostsforlearning;

public class Country {

 String countryName;
 
 public String getCountryName() {
  return countryName;
 }
 
 
 public void setCountryName(String countryName) {
  this.countryName = countryName;
 }
 
}


2.ScopesInSpringMain.java

This class contains main function.Create ScopesInSpringMain.java under package org.arpit.javapostsforlearning.Copy following content into ScopesInSpringMain.java
package org.arpit.javapostsforlearning;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopesInSpringMain{

 public static void main(String[] args) {
  
  ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
  Country countryObj1 = (Country) appContext.getBean("country");
  countryObj1.setCountryName("India");
  System.out.println("Country Name:"+countryObj1.getCountryName());
  
  //getBean called second time
  Country countryObj2 = (Country) appContext.getBean("country");
  System.out.println("Country Name:"+countryObj2.getCountryName());
 }
}

3.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:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="country" class="org.arpit.javapostsforlearning.Country">
 </bean>
</beans>

4.Run it

When you will run above application,you will get following as output.

Country Name:India
Country Name:India

When We firstly called getBean and retrieved country object and set countryName to "india" and when second time we called getBean method it did nothing but returned same object with countryName as "india".

Prototype bean scope:



 Now we will make change in above xml configuration file.We will add scope attribute in <bean> tag and set it to "prototype" and then run it again

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:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="country" class="org.arpit.javapostsforlearning.Country" scope="prototype">
 </bean>
</beans>

Run it again:

When you will run above application,you will get following as output.
Country Name:India
Country Name:null

When We firstly called getBean and retrieved country object and set countryName to "india" and when second time we called getBean method it returned new object with countryName as "null".

Source code:

For singleton example:


For prototype example:

In next post,we will see initializing collections in spring.
 

Java tutorial for beginners Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates