URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Java
  HTML https://java.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: General Discussion
       *****************************************************
       #Post#: 6--------------------------------------------------
       Spring Bean Scopes
       By: shekar Date: September 10, 2014, 8:11 am
       ---------------------------------------------------------
       Spring Bean Scopes:
       In Spring, bean scope is used to decide which type of bean
       instance should be return from Spring container back to the
       caller.
       Spring framework supports five type of scopes.
       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.
       singleton :
       singleton is the default scope.
       In the bean definition if the scope is not given, then by
       default singleton scope is assumed.
       In a given Spring container a singleton scoped bean will be
       instantiated only once and the same will be used for its
       lifetime.
       If no bean scope is specified in bean configuration file,
       default to singleton.
       Singleton example:
       <beans xmlns="
  HTML http://www.springframework.org/schema/beans"
       xmlns:xsi="
  HTML http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
  HTML http://www.springframework.org/schema/beans
       
  HTML http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
       
       <bean id="customerService"
       class="com.mkyong.customer.services.CustomerService"
       />
       
       </beans>
       prototype scope:
       prototype scope allows the bean to be instantiated whenever it
       is requested.
       Every time a separate instance is created, just opposite to
       singleton.
       Stateful beans which hold the conversational state should be
       declared as prototype scope.
       If you want a new ‘customerService’ bean instance, every time
       you call it, use prototype instead.
       Prototype example:
       <beans xmlns="
  HTML http://www.springframework.org/schema/beans"
       xmlns:xsi="
  HTML http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
  HTML http://www.springframework.org/schema/beans
       
  HTML http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
       
       <bean id="customerService"
       class="com.mkyong.customer.services.CustomerService"
       scope="prototype"/>
       
       </beans>
       request scope:
       Spring bean configured as request scope instantiates the bean
       for a single HTTP request.
       The instantiated object lives through the HTTP request. This is
       available only for web-aware spring application context.
       4. session scope
       session scope is very similar to HttpSession scope. Beans
       instantiated based on session scope scope lives through the HTTP
       session. Similar to request scope, it is applicable only for web
       aware spring application contexts.
       global_session scope:
       global_session scope is equal as session scope on portlet-based
       web applications. This scope is also applicable only for web
       aware spring application contexts. If this is global_session is
       used in normal web application (not in portlet), then it will
       behave as session scope and there will not be any error.
       Bean scopes annotation:
       import org.springframework.context.annotation.Scope;
       import org.springframework.stereotype.Service;
       
       @Service
       @Scope("prototype")
       public class CustomerService
       {
       String message;
       
       public String getMessage() {
       return message;
       }
       
       public void setMessage(String message) {
       this.message = message;
       }
       }
       Enable auto component scanning
       <beans xmlns="
  HTML http://www.springframework.org/schema/beans"
       xmlns:xsi="
  HTML http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="
  HTML http://www.springframework.org/schema/context"
       xsi:schemaLocation="
  HTML http://www.springframework.org/schema/beans
       
  HTML http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       
  HTML http://www.springframework.org/schema/context
       
  HTML http://www.springframework.org/schema/context/spring-context-2.5.xsd">
       
       <context:component-scan base-package="com.macys.customer"
       />
       
       </beans>
       *****************************************************