Drop Down

Saturday, September 21, 2019

Spring_App_7_CustomScope

package com.amit.beans;

public class HelloBean
{
public HelloBean()
{
System.out.println("Hello bean created....");
}

}

=========================================================================
package com.amit.customScope;

import java.util.HashMap;

public class MyThreadLocal extends ThreadLocal 
{
@Override
protected Object initialValue()  //executed when we call getMethod() but no set value
{
  return new HashMap<String,Object>();
}

}

=======================================================================
package com.amit.customScope;

import java.util.HashMap;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.beans.factory.config.Scope;

public class ThreadScope implements Scope 
{
MyThreadLocal threadlocal = new MyThreadLocal();
HashMap<String,Object> scope = null; // to get scope object

@Override
public Object get(String name, ObjectFactory<?> objFactory)  //return bean object (available in the thread scope), if not available create a new bean object keep it in scope and return it.
{ //ObjectFactory class is in Spring used to create Bean Object
scope = (HashMap<String,Object>) threadlocal.get();  //calls initialValue() if no object is created
Object obj = scope.get(name);     // to check if it return object 
         if(obj == null)
         {
        obj = objFactory.getObject();
        scope.put(name, obj);                                  //keep obj in scope hashmap
         } 
return obj;
}

@Override
public String getConversationId() // to get id for bean e.g session id
{
return null;
}

@Override
public void registerDestructionCallback(String arg0, Runnable arg1) //when bean destroyed
{

}

@Override
public Object remove(String name) //to remove bean obj from scope
{
  Object obj = scope.remove(name);
return obj;
}

@Override
public Object resolveContextualObject(String arg0) // Resolves --> if 1 key is associated with multiple context object 
{
return null;
}

}

========================================================================
<?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 id="helloBean" class ="com.amit.beans.HelloBean" scope="thread"/>
<bean id="customThreadScope" class ="com.amit.customScope.ThreadScope" />
<bean id ="customScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
    <map>
         <entry key="thread" value-ref = "customThreadScope">
         </entry>
    </map>
</property>
</bean>

</beans>        
========================================================================
package com.amit.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.amit.beans.HelloBean;

public class Test {

public static void main(String[] args) 
{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloBean bean1 = (HelloBean)context.getBean("helloBean");
  System.out.println("bean1 "+bean1);
  
  HelloBean bean2 = (HelloBean)context.getBean("helloBean");
  System.out.println("bean2 "+bean2);
}

}

No comments:

Post a Comment

Java 8 Notes Pics