
1. Overview
In this article, we will learn about the differences between the Spring Application scope vs Singleton.
2. Spring bean
Bean: An object that is instantiated, assembled, and managed by the Spring container. In other words, a bean is a class instance controlled by the Spring framework. See our bean instantiation article to understand more about bean instantiation.
Dependency: A class object requires objects of other classes to perform its responsibilities. We call them dependencies.
2.1. Spring application scope vs singleton
A ServletContext
is shared between all servlets living on the same servlet container (e.g. Tomcat). This is a Java EE class and belongs to the package javax.servlet
.
An ApplicationContext
represents a Spring IoC container, so it is a Spring-specific class and belongs to the package org.springframework.context
. The Singleton scoped beans are bounded to the ApplicationContext whereas the Application scoped beans are bounded to the ServletContext
.
The Singleton scope is the default scope in Spring.
You can have multiple IoC containers in the same Servlet container, so you can have multiple singleton beans of the same type but only one application scope bean of each type.
3. Spring Bean scope
A Bean definition describes a bean instance, which has property values, constructor argument values, and additional information such as the scope.
You can create many object instances from a single bean definition.
Also, you can control the scope of the object created from a particular bean definition. You can define beans to deploy in one of several scopes. The Spring Framework supports six scopes and you can also create a custom scope.
Let’s discuss the Application scope.
3.1. Application Scope
For example, the following is the bean definition of the AppPreferences
class using XML configuration:
<bean id="appPref" class="com.tedblob.AppPreferences" scope="application"/>
The Spring container creates a new instance of the AppPreferences
bean by using the appPref
bean definition once for the entire web application. As mentioned earlier, the appPref
bean is scoped at the ServletContext
level and stored as a regular ServletContext
attribute.
When using annotation-driven components or Java configuration, you can use the @ApplicationScope
annotation to assign a component to the application
scope.
For example, the following AppPreferences
are created by the Spring IoC container and scoped to the ServletContext
.
@ApplicationScope @Component public class AppPreferences { // ... }
3.2. Singleton scope
When you define a bean definition and scoped it as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. Spring stores the single instance in a cache of such singleton beans, and all subsequent requests and references for that named bean return the same cached object.
The following image shows how the singleton scope works:

The scope of the Spring singleton is best described as being per container and per bean.
If you have specified no scope explicitly in your bean definition, then this singleton scope is the default scope.
Spring’s concept of a singleton bean differs from the singleton pattern as defined in the Gang of Four (GoF) patterns.
A Singleton pattern ensures that one and only one instance exists per ClassLoader
. Here, Singleton scope ensures only one bean instance for a bean definition per container. Each container has its own instance of singleton bean.
4. Conclusion
To sum up, we have learned about the Spring Application scope vs Singleton. You can find code samples of our articles in our GitHub repository.