One of the speciality of spring framework is the options it provide for customization. In the last article we saw about spring bean scopes available as part of standard implementation. In this article we shall see about custom scope that is available from Spring 2.0
Based on spring custom scope, we can define new scope as well as modify out of the box provided spring bean scopes. We will not be able to modify the standard singleton and prototype scopes but we can modify the request, session and global_session scopes. Though spring framework provides option to modify the existing scopes, its better not to touch it and also I don’t see any real use case that asks for overriding existing scopes.
Example scenarios where we can use spring bean custom scope are in,
org.springframework.beans.factory.config.Scope is an interface and by implementing it we create a custom scope in the spring container. This Scope interface contains four methods,
Out of the above four get() is the mandatory operation and others are optional.
public class ThreadScope implements Scope { private final ThreadLocal threadScope = new ThreadLocal() { protected Object initialValue() { return new HashMap(); } }; public Object get(String name, ObjectFactory objectFactory) { Map scope = (Map) threadScope.get(); Object object = scope.get(name); if(object==null) { object = objectFactory.getObject(); scope.put(name, object); } return object; } public Object remove(String name) { Map scope = (Map) threadScope.get(); return scope.remove(name); } public void registerDestructionCallback(String name, Runnable callback) { } }
Once we create the custom spring bean scope we need to register it with the spring container. There are two ways to do it. Either through programmatic registration or using conventional XML based configuration. For programmatic registration, we should get the bean factory and call the registerScope method as below,
Scope customScope = new ThreadScope(); beanFactory.registerScope("thread", customScope);
For XML based configuration to register,
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="thread"> <bean class="com.javapapers.spring.ThreadScope"/> </entry> </map> </property> </bean>
<bean id="..." class="..." scope="thread"/>
You know something, Spring 3.0 comes with thread scope the class name is SimpleThreadScope.
Comments are closed for "Custom Scope for Spring Bean".
good one!!
NICE………..
Joe,
A great article, similar of it’s kind, I was looking forward!! Knowledge share about SimpleThreadScope is real good! Appreciate your efforts!!
Nice Article…………Joe!
nice
this article is as simple as it can be .thanks Joe.
Joe I also request you and other users reading this to share production scenarios/examples where they needed a custom scope .
nice joe
Hi friends
I am seeking help from u guys for the below issue in spring MVC.
” Neither BindingResult nor plain target object for bean name available as request attribute ”
please help me out on this
Thanks in Advance….
Hi joe ,
can you please write on Aspect Oriented programming.
-Ankur
Thanks to provide better explanation.
simple and awesome