Custom Scope for Spring Bean

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,

  1. creating a bean scope where beans can be shared between servlet contexts
  2. creating a bean scope where beans can be shared within a same thread
  3. and a lot more spring custom scope example
  4. out of all these, I like the idea of having a custom scope for a Locale

1. Create the custom bean scope

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) {
}

}

2. Register the created custom bean

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>

3. Use the custom scope

<bean id="..." class="..." scope="thread"/>

You know something, Spring 3.0 comes with thread scope the class name is SimpleThreadScope.

This Spring tutorial was added on 21/11/2012.

Comments on "Custom Scope for Spring Bean" Tutorial:

  1. sikandar says:

    good one!!

  2. C STHIYARAJA says:

    NICE………..

  3. Anonymous says:

    Joe,

    A great article, similar of it’s kind, I was looking forward!! Knowledge share about SimpleThreadScope is real good! Appreciate your efforts!!

  4. Ramesh says:

    Nice Article…………Joe!

  5. srirambal says:

    nice

  6. raja says:

    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 .

  7. Anwar says:

    nice joe

  8. KARTHIK says:

    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….

  9. Ankur says:

    Hi joe ,

    can you please write on Aspect Oriented programming.

    -Ankur

  10. Anonymous says:

    Thanks to provide better explanation.

  11. siddhartha says:

    simple and awesome

Comments are closed for this "Custom Scope for Spring Bean" tutorial.