Monday 5 October 2015

How to create custom scope in spring?

Custom Scope in Spring:-

As per the Spring doc As of Spring 2.0, the bean scoping mechanism is extensible. You can define your own scopes, or even redefine existing scopes, although the latter is considered bad practice and you cannot override the built-in singleton and prototype scopes
A custom scope typically corresponds to a backing store that can manage object instances. In this case, Spring provides its familiar programming model, enabling injection and look-up, and the backing store provides instance management of the scoped objects. Typical backing stores are:HTTP session, Clustered cache, Other persistent store
The need to create a Custom Scope actually depends on the problem at hand. For instance, you might want to create a pre-defined number of instances of a particular bean, but not more than that. So until this number is met, you keep creating new instances, but once the number is met, you return existing instances in a balanced manner. This is just one context or instance when we can use Custom Scopes. As said, the usage depends on the problem at hand.
Implement Custom Scope bean:-
To integrate your custom scope(s) into the Spring container, you need to implement theorg.springframework.beans.factory.config.Scope interface. Create the custom scope com.tick.ApnaScope under directory src/com/tick which implements Scope interface. The contents of the file are as below:
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

public class MyScope implements Scope {
 private Map<String, Object> objectMap = Collections
   .synchronizedMap(new HashMap<String, Object>());

 public Object get(String name, ObjectFactory<?> objectFactory) {
  if (!objectMap.containsKey(name)) {
   objectMap.put(name, objectFactory.getObject());
  }
  return objectMap.get(name);

 }

 public Object remove(String name) {
  return objectMap.remove(name);
 }
 public String getConversationId() {
  return "ApnaScope";
 }

 /**
  * clear the beans
  */
 public void clearBean() {
  objectMap.clear();
 }
}

public class Person {
4    /**
5     * Default Constructor
6     */
7    public Person() {
8        System.out.println("*** Person() constructor ***");
9    }
10}

<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4    xmlns:aop="http://www.springframework.org/schema/aop"
5    xsi:schemaLocation="http://www.springframework.org/schema/beans
6    http://www.springframework.org/schema/beans/spring-beans.xsd
7    http://www.springframework.org/schema/aop
8    http://www.springframework.org/schema/aop/spring-aop.xsd">
9 
10   <bean id="apnaScope" class="com.tick.MyScope"/>
11 
12    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
13        <property name="scopes">
14            <map>
15                <entry key="customscope">
16                    <ref bean="apnaScope" />
17                </entry>
18            </map>
19        </property>
20    </bean>
21 
22    <bean name="p1" class="com.tick.Person" scope="customscope" />
23 
24</beans>
Few things to notice here:
MyScope is defined as a bean here with name customscope. Application can lookup this bean to remove the short lived beans.
MyScope is registered with the Spring IoC container using CustomScopeConfigurer with scope name myScope. The key will be the name of the Custom Scope to be used in the bean definition and the value will be bean which implement the scope.
Finally, the Person bean is defined with scope customscope
TEST IT and See the result....



No comments:

Post a Comment