• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Spring bean scopes

July 19, 2012 By j2eereference Leave a Comment

Here we will see different scope on spring managed beans.There are five types of scope which will support by spring

  1. singleton
  2. prototype
  3. request
  4. session
  5. global session

Now lets look into each of these sessions in detail.

singleton scope:

This is default scope for all the beans defined in the spring configuration. If you specify a bean’s scope as singleton , Spring will create only one instance and will share with all the  subsequent request for this instance.There will be only one instance for a particular bean if we declare it as singleton.Once a bean instance created it is stored in the cache and used for the all the incoming requests for the same bean.

Declaration:

XHTML
1
<bean id="sampleClass" class="com.j2eereference.SampleClass" scope="singleton"/>

With the above bean definition the Spring container will create a brand new instance of  SampleClass bean only for the one time .

prototype scope:

In this scope, container will create a new instance for every request.

Declaration :

XHTML
1
<bean id="sampleClass" class="com.j2eereference.SampleClass" scope="prototype"/>

With the above bean definition the Spring container will create a brand new instance of the SampleClass  bean  for each and every new request.

request scope:

This scope use only in web-based applications.

This is similar as HttpServletRequest.When new request comes in, the new instance will be created. Request Scope beans are created for every request.Here the object’s lifetime ends when the request is completed.

Declaration :

XHTML
1
<bean id="sampleClass" class="com.j2eereference.SampleClass" scope="request"/>

With the above bean definition the Spring container will create a brand new instance of the SampleClass bean  for each and every HTTP request.

session Scope:

This scope use only in web-based applications.

It is similar to the HttpSession . Here beans are created and stored for each session. When new request comes in, the new instance will be created and the object’s lifetime ends when the session is closed.

Declaration :

XHTML
1
<bean id="sampleClass" class="com.j2eereference.SampleClass" scope="session"/>

With the above bean definition the Spring container will create a brand new instance of the SampleClass bean  for each and every new session.

global session scope :

This scope use only in web-based applications.

The global session scope is similar to the Session scope and really only makes sense in the context of portlet-based web applications. The portlet specification defines a global Session that is shared by all of the various portlets that make up a single portlet web application. Beans defined at the global session scope are available till the global portlet Session is active.

Declaration :

XHTML
1
<bean id="sampleClass" class="com.j2eereference.SampleClass" scope="globalSession"/>

With the above bean definition the Spring container will create a brand new instance of the SampleClass bean  for each and every new global session.

Related Posts

  • Spring JSR-250 Annotations
  • Injecting singleton class with spring
  • Writing First Aspect in spring
  • What is Aspect Oriented Programming (AOP)
  • Autowired Annotation
  • Constructor injection in spring
  • Bean autowiring
  • LifeCycle call backs in Spring
  • BeanPostProcessor in Spring
  • Initializing list of values using Spring

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.