Spring Framework: Scoping Beans
Till now we have discussed wiring and injection of beans. In this article we will take a look at the scope of the beans declared in Spring. Scope of the bean is the nature of the bean.
A bean declared in Spring context is singleton by default, no matter if it is declared using XML tags or annotations. This means whenever the declared bean will be injected into another bean or whenever its instance will be retrieved using its getter, it will return the exact same instance. Spooky, isn’t it.
Now, sometimes you do not want your bean to have this behavior. Sometimes, you want your bean to return a unique instance each time it is called. Luckily, that is also possible with Spring.
Spring gives you multiple scopes to choose from, depending upon your requirement, while declaring your beans. Lets go ahead with a real life example:
Lets say you are arranging a wedding. At wedding, the important thing you need to take care of are wedding crashers. They can get in if proper steps are not taken.
Luckily you have come up with a plan, you have made it mandatory for every person attending the wedding to have a wedding invitation so you would know if you invited the person or not. Now each guest will have a unique instance of wedding invitation.
When we will implement this scenario in Spring, we will have a class named WeddingInvitation. We declare this invitation as we have declared the beans in previous article, just as given below:
<bean id=”invitation” class=”com.aurorasolutions.springframework.beans.WeddingInvitation” />
This declaration has a drawback. This instance of WeddingInvitation is singleton, which means each guest will have the same instance of wedding invitation. But if we declare this bean with an extra option ‘scope’, as given below, then we can achieve what we are aiming for, unique invitation for each guest:
<bean id=”invitation” class=”com.aurorasolutions.springframework.beans.WeddingInvitation” scope=”prototype”/>
Now with this declaration, each guest in our wedding will have a unique instance. Beware crashers!
Spring does not stop at these two scopes. It has total of 5 scopes for you to choose from.
1. singleton: A bean declared with this scope follows singleton pattern. There is only one instance existing for such a bean and every call to this bean returns the exact same instance.
2. prototype: A bean with this scope can have as many number of instance as possible. Each call to this bean returns a unique instance.
3. request: A bean with this scope will have a unique instance for each HTTP request. That means each and every HTTP request will have its own instance of the bean.
4. session: A bean with this scope means each and every HTTP session will have its own unique instance of the bean.
5. global-session: This scope is valid in Portlet Context only. It means global HTTP session will have a unique instance of the bean.
It is worth remembering that aside from singleton and prototype scope, every other scope is valid with web-aware Spring ApplicationContext only.
It should be noted that Spring’s singleton are nothing like true singleton objects. Spring’s singletons are only singleton within the given Application Context.