Creating custom scopes

For creating your customer scope in the Spring IoC container, Spring provides the org.springframework.beans.factory.config.Scope interface. You have to implement this interface to create your own custom scopes. Take a look at the following MyThreadScope class as a custom scope in the Spring IoC container:

    package com.packt.patterninspring.chapter5.bankapp.scope; 
    import java.util.HashMap; 
    import java.util.Map; 
    import org.springframework.beans.factory.ObjectFactory; 
    import org.springframework.beans.factory.config.Scope; 
 
    public class MyThreadScope implements Scope { 
      private final ThreadLocal<Object> myThreadScope = new
ThreadLocal<Object>() { protected Map<String, Object> initialValue() { System.out.println("initialize ThreadLocal"); return new HashMap<String, Object>(); } }; @Override public Object get(String name, ObjectFactory<?> objectFactory) { Map<String, Object> scope = (Map<String, Object>)
myThreadScope.get(); System.out.println("getting object from scope."); Object object = scope.get(name); if(object == null) { object = objectFactory.getObject(); scope.put(name, object); } return object; } @Override public String getConversationId() { return null; } @Override public void registerDestructionCallback(String name, Runnable
callback) { } @Override public Object remove(String name) { System.out.println("removing object from scope."); @SuppressWarnings("unchecked") Map<String, Object> scope = (Map<String, Object>)
myThreadScope.get(); return scope.remove(name); } @Override public Object resolveContextualObject(String name) { return null; } }

In the preceding code, we have overridden multiple methods of the Scope interface as follows:

  • Object get(String name, ObjectFactory objectFactory): This method returns the object from the underlying scope
  • Object remove(String name): This method removes the object from the underlying scope
  • void registerDestructionCallback(String name, Runnable destructionCallback): This method registers the destruction callbacks, and is executed when the specified object with this custom scope is destroyed

Now let's see how to register this custom scope with the Spring IoC container, and how to use it in the Spring application.

You can register this custom bean scope with the Spring IoC container declaratively by using the CustomScopeConfigurer class as follows :

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.
config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="myThreadScope"> <bean class="com.packt.patterninspring.chapter5.
bankapp.scope.MyThreadScope"/> </entry> </map> </property> </bean> <bean id="myBean" class="com.packt.patterninspring.chapter5.
bankapp.bean.MyBean" scope="myThreadScope"> <property name="name" value="Dinesh"></property> </bean> </beans>

As you can see in the preceding configuration file, I have registered my custom bean scope named myThreadScope with the application context by using the CustomScopeConfigurer class. This custom scope that I am using is similar to the singleton or prototype scope through the scope attribute of the bean tag in the XML configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset