Spring Bean Scopes

Spring framework supports five type of scopes and for bean instantiation as of Spring 3.0 and also we can create a custom scope.

  • singleton
  • prototype
  • request
  • session
  • global_session

singleton This bean scope is default and it enforces the container to have only one instance per spring container irrespective of how much time you request for its instance.

prototype This bean scope just reverses the behavior of singleton scope and produces a new instance each and every time a bean is requested.

request With this bean scope, a new bean instance will be created for each web request made by client. As soon as request completes, bean will be out of scope and garbage collected.

session Just like request scope, this ensures one instance of bean per user session. As soon as user ends its session, bean is out of scope.

Global-session This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.

How to define the bean scope ?

  • In bean configuration file
  • Using annotations

Using configuration file

<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="demoBean" 
class="com.manish.application.web.BeanDemo" 
scope="session" />
  
</beans>

Using Annotation

@Service
@Scope("session")
public class Beanemo
{
  ...
}

Singleton Scope Example

Files required are as follow :

  • Welcome.java
  • MainApp.java
  • Beans.xml

Welcome.java Class

package com.javasafari;

public class Welcome {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Message Is: " + message);
   }
}

MainApp

package com.javasafari;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
  new ClassPathXmlApplicationContext("Beans.xml");
  Welcome obj1 = (Welcome) context.getBean("welcome");

  obj1.setMessage("Welcome To Spring");
      obj1.getMessage();

   Welcome obj2 = (Welcome) context.getBean("welcome");
      obj2.getMessage();
   }
}

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

   <bean 
id="welcome" class="com.javasafari.Welcome" 
      scope="singleton">
   </bean>

</beans>

Output

Message Is: Welcome To Spring
Message Is: Welcome To Spring

Prototype Example

Files required are as follow :

  • Welcome.java
  • MainApp.java

These class are same as in abobe example

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

   <bean id=
"welcome" class="com.javasafari.Welcome" 
      scope="prototype">
   </bean>

</beans>

Output

Message Is: Welcome To Spring
Message Is: null