Friday 25 August 2017

What are different Spring Bean Scopes?

       The core of the Spring framework is a Bean Factory and it's mechanism to create and manage the beans inside the Spring containers.  In bean, attribute Scope is referred to what kind of object has to create and returned to the container.
       In Spring, there are five bean scopes are available, three of which are available only if you use web-aware ApplicationContext.  Bean scopes are singleton,  prototype, request, session and global session.


Spring Bean Scopes
Spring Bean Scopes

  • singleton

The Spring IOC container can create only one instance per container irrespective of how many times you request for this instance. This is the default bean scope. This singleton behavior is maintained by bean factory itself.

Example:- EmployeeService.java,

package com.adnblog.employee;

public class EmployeeService { 
 
     String message;

     public String getMessage() {
           return message;
     }

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

Spring bean configuration file : spring-employee.xml (if scope is not declared, default it should be 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="employeeService"
            class="com.adnblog.employee.EmployeeService" />

</beans>

Spring code - Main Program
 
package com.adnblog;

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

import com.adnblog.employee.EmployeeService;

public class MainClass { 
 
       public static void main( String[] args ) { 
 
              ApplicationContext context =
                               new ClassPathXmlApplicationContext(new String[] {"spring-employee.xml"});

              EmployeeService empService = (EmployeeService)context.getBean("employeeService");
              empService.setMessage("empService Message");
              System.out.println("Message : " + empService.getMessage());

              //trying to create second instance
             EmployeeService empService1 = (EmployeeService)context.getBean("employeeService");
             System.out.println("Message : " + empService1.getMessage());
      }
}

Output Message : empService Message
                Message : empService Message


  • prototype

     The Spring IOC container creates new bean instance of the object at every time request for that specific bean is made. 
   To achieve prototype scope, declare scope attribute as prototype in the bean configuration file as below,

<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="employeeService"
            class="com.adnblog.employee.EmployeeService"  scope="prototype"/>

</beans>

Run the main program, you will get output  

Output : Message : empService Message
               Message : null


  • request

       In 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.  This scope only available web aware ApplicationContext i.e WebApplicationContext.

  • session

     It is also like request scope, this ensures one instance of bean per user session. As soon as user ends its session, bean is out of scope.  This scope only available web aware ApplicationContext i.e WebApplicationContext.

  •  global-session

      This is something which is connected to Portlet applications. When your application works in Portlet container it is built of some amount of portlets. Each portlet has its own session, but if your want to store variables global for all portlets in your application than you should store them in global-session. This scope doesn’t have any special effect different from session scope in Servlet based applications.

No comments:

Post a Comment