Spring Bean Scopes
Introduction:
Spring core container instantiates beans and manages their life-cycle. While defining a bean, we can provide its scope. Unless explicitly provided, singleton is the default scope of beans in a Spring container.
We have five types of bean scopes available in Spring. In this tutorial, we’ll explore each of them.
1. singleton:
The singleton scope ensures that there exists only a single instance of that bean per Spring IoC container. This single bean instance is stored in the cache of all singleton beans and the reference to this cached object is returned for each subsequent requests.
As we know, all Spring beans are singleton by default.
Let’s say we have a Student bean:
@Component public class Student { private int id; private String name; //constructor, getters, setters }
The Student class will be a singleton bean by default. Let’s try to load it in our main() method:
public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml"); Student student1 = (Student) context.getBean("student"); student1.setName("John"); System.out.println(student1.getName()); //Prints 'John' Student student2 = (Student) context.getBean("student"); System.out.println(student2.getName()); //Prints 'John' } }
Clearly, the existing instance of Student bean has been returned for the second invocation. In other words, both student1 and student2 references are pointing to the same object.
2. prototype:
For a prototype-scoped bean, a new instance is created for every request of that specific bean within our application code.
We can either configure our bean’s scope as a prototype using @Scope annotation:
@Component @Scope("prototype") public class Student { ... }
Or when using XML-based configurations, we’ll have:
<bean id = "student" class="org.programmergirl.domain.Student" scope="prototype"> </bean>
Now, let’s see what happens in our main() method:
public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml"); Student student1 = (Student) context.getBean("student"); student1.setName("John"); System.out.println(student1.getName()); //Prints 'John' Student student2 = (Student) context.getBean("student"); System.out.println(student2.getName()); //Prints null } }
Here, student2 refers to a new instance of Student class with its name attribute as null.
Note that we should always use prototype scope for the stateful beans and singleton for the stateless beans.
3. request:
When using the request scope, a new instance is created for each and every HTTP request. The bean instance is destroyed on the HTTP request completion.
We can configure the request scope either using annotation:
@Component @Scope("request") public class Student { ... } //Or @Component @RequestScope public class Student { ... }
Or via XML configuration:
<bean id = "student" class="org.programmergirl.domain.Student" scope="request"> </bean>
4. session:
For a session scope, the Spring container creates a new bean instance for each HTTP session. All the HTTP requests within an HTTP session will share the same bean instance.
To configure a session-scoped bean, we’ll use:
@Component @Scope("session") public class Student { ... } //Or @Component @SessionScope public class Student { ... }
For a xml-based configurations we’ll have:
<bean id = "student" class="org.programmergirl.domain.Student" scope="session"> </bean>
The container will destroy the session-scoped bean instances as and when that HTTP session closes.
5. application:
With application scope, a web-aware container creates a bean instance per ServletContext. Note that the application scope is different from the singleton scope:
- We can use the singleton scope in a standalone application. The application scope is only valid for a web-based Spring container/application
- For singleton scope, a single instance exists per application context. On the other hand, the application-scoped bean has a single instance for the entire ServletContext. We might have multiple applications sharing the same ServletContext
Let’s configure our Student bean as an application-scoped bean:
@Component @Scope("application") public class Student { ... } //Or @Component @ApplicationScope public class Student { ... }
The xml based definition would look like:
<bean id = "student" class="org.programmergirl.domain.Student" scope="application"> </bean>
Conclusion:
In this tutorial, we explored various Spring bean scopes.
Note that the request, session and application bean scopes are only valid for a web-aware Spring ApplicationContext.
Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: Spring Bean Scopes Opinions expressed by Java Code Geeks contributors are their own. |