Friday, 20 December 2013

Spring bean scopes

Spring bean scope is used to decide which type of bean instance should be return from Spring container.

5 types of bean scopes supported :

  1. singleton – Return a single bean 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 most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.



Singleton vs Prototype

Here’s an example to show you what’s the different between bean scope : singleton and prototype.


package com.mahesh.user.services;
 
public class UserService 
{
 String message;
 
 public String getMessage() {
  return message;
 }
 
 public void setMessage(String message) {
  this.message = message;
 }
}

1. Singleton example

If no bean scope is specified in bean configuration file, default to singleton.


<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="userService" 
            class="com.mahesh.user.services.UserService" />
 
</beans>
 
 

Run it

package com.mahesh.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.mahesh.user.services.UserService;
 
public class App 
{
    public static void main( String[] args )
    {
     ApplicationContext context = 
      new ClassPathXmlApplicationContext(new String[] {"Spring-User.xml"});
 
     UserService userA = (UserService)context.getBean("userService");
     userA.setMessage("Message by userA ");
     System.out.println("Message : " + userA.getMessage());
 
     //retrieve bean again
     UserService userB = (UserService)context.getBean("userService");
     System.out.println("Message : " + userB.getMessage());
    }
}
 
 

Output

Message : Message by userA
Message : Message by userA
 
Since the bean ‘userService’ is in singleton scope, the second 
retrieval by ‘userB’ will display the message set by ‘userA’ also, even 
it’s retrieve by a new getBean() method. In singleton, only a single 
instance per Spring IoC container, no matter how many time you retrieve 
it with getBean(), it will always return the same instance.
 
 

2. Prototype example 

 If you want a new ‘userService’ bean instance, every time you call it, use prototype instead.


<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="userService" class="com.mahesh.user.services.UserService" scope="prototype"/>   </beans>
  

Run it again

 
Message : Message by userA
Message : null
 
In prototype scope, you will have a new instance for each getBean() method called.
 
 

 

 

 

 
 

No comments:

Post a Comment