Spring Book – Chapter 3 – Spring Quick Start

Bean Scoping

In Spring, bean scope is used to decide which type of bean instance should be returned from Spring container back to the caller. Spring Framework supports exactly five scopes (of which three are available only if you are using a Web-aware ApplicationContext). They are:

  • singleton—Return a single bean instance per Spring IoC container
  • prototype—Return a new bean instance each time when requested
  • request—Return a single bean instance per HTTP request
  • session—Return a single bean instance per HTTP session
  • globalSession—Return a single bean instance per global HTTP session.

NOTE: Some scopes, namely request, session, and globalSession, are available only inside a Web-aware context.


Singleton is the default scope for all the beans defined in Spring’s configuration files. If a specified bean is declared a singleton, the Spring container will create only one instance of the bean. All the requests coming to that bean will be served by the same instance, and there won’t any other instances created for the same bean. The created bean instance is stored in the cache and used for the all the incoming requests for the same bean. The singleton-design pattern concept and the way Spring handles singleton beans are different. Singleton class creates only one instance per the classloader: Spring creates one instance for each Spring container. If multiple Spring containers are running inside a single classloader, the singleton beans are specific to the container for which they are created. The scope of the Spring singleton is best described as per container and per bean.


This creates a new instance for every request. These types of beans are more suitable for creating stateful beans. When using this scope, it is the developer’s responsibility to handle the many callback methods. Specifically, the application developer has to handle all the destruction callbacks programmatically, because the container just hands over the beans to the application. The important thing is that after this, the container doesn’t have any control of the prototype beans. This scope, available for both BeanFactory and ApplicationContext, scopes a single bean definition to any number of object instances.


This scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request will have its own instance of a bean created off the back of a single bean definition. It is similar to HttpServletRequest, in which the object’s lifetime ends when the request is completed. When a new request comes in, a new instance will be created. As indicated earlier, this scope is only valid in the context of a Web-aware Spring ApplicationContext, such as XmlWebApplicationContext. If you try using these next scopes with regular Spring IoC containers, such as the XmlBeanFactory or ClassPathXmlApplicationContext, you will get an IllegalStateException complaining about an unknown bean scope.


With this type, beans are created and stored for each session. It is similar to HttpSession, in which the object’s lifetime ends when the session is closed. As indicated earlier, this scope is valid only in the context of a Web-aware Spring ApplicationContext, such as XmlWebApplicationContext.


This scopes a single bean definition to the lifecycle of a global HTTP Session. The beans defined as Global Session are stored in the javax.servlet.http.HttpSession object and are only valid in the context of a Web-aware Spring ApplicationContext, such as org.springframework.web.context.suport.XmlWebApplicationContext.

Examination Tip: You need to thoroughly understand what is meant by BeanFactory and ApplicationContext in a Spring container and which bean scope makes sense in these contexts.

BeanFactory—Instantiation of bean happens when we invoke the getBean(“loyaltyService”) method; hence, it is called Lazy.

ApplicationContext — Instantiation happens when the context is up; hence, it is called Smart.

Custom Scopes

The bean scoping mechanism in Spring is also extensible. You are not limited to just the bean scopes that Spring provides out-of-the-box; you can also define your own scopes, or even redefine the existing scopes. (The latter option would probably be considered bad practice; please note that you cannot override the built-in singleton and prototype scopes.)

Creating your own custom scope

Scopes are defined by the org.springframework.beans.factory.config.Scope interface. This is the interface that you will need to implement in order to integrate your own custom scope(s) into the Spring container. The scope interface has four methods dealing with getting objects from the scope, removing them from the scope, and allowing them to be “destroyed” if needed.

Using a custom scope

After writing a new custom scope, you need to make the Spring container aware of your new scope(s). The central method for registering a new scope with the Spring container is declared on the org.springframework.beans.factory.config.ConfigurableBeanFactory interface. The main method central to this interface is shown in Listing 3-25.

Listing 3-25. Method used to register newly created scope

The first argument to the registerScope(..) method is the unique name associated with a scope. The second argument to the registerScope(..) method is an actual instance of the custom-scope implementation that you wish to register and use. After this, you will have to register it as shown in the Listing 3-26 (Java Configuration of new custom scope). This can be done in the place where you actually get your applications ApplicationContext object.

Listing 3-26. Creating new custom scope

Listing 3-27 shows how to register your own custom scope implementation(s) programmatically; you can also do this declaratively using the CustomScopeConfigurer class as shown in Listing 3-28.

Listing 3-27. Registering the newly custom scope

Listing 3-28. Custom scope implementation using CustomScopeConfigurer

Note: The ThreadScope class is the new scope class that we have written implementing the Scope interface.

You can then create bean definitions that adhere to the scoping rules of your new custom scope, as shown in Listing 3-29:

Listing 3-29. Creating bean definition in your custom scope


In this chapter, I have just touched upon the main concepts and explained in detail certain key aspects and features of Spring that you need to understand in order to proceed with advanced features of Spring in action later on. You should now know how Spring works, what a bean is, what the various bean lifecycle events are, and the various bean scopes you can use in application. You have also seen that instead of using scopes provided by Spring, you can define your own scope or even redefine existing scopes. You also should have a clear understanding of Dependency Injection (DI) and the various types used in Spring Framework.

Page Visitors: 2556

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *