Announcement Announcement Module
Collapse
No announcement yet.
Creating scope and proxy class programmatically Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating scope and proxy class programmatically

    Hi all,
    I write code in order to create custom scope, registering it and creating a scoped proxy all programmatically. The code works, but i would know if is correct.
    All the code i've writed is in a filter servlet (for now) in the init method.

    The steps are:
    1) Creating a proxy (for a class, in ny case) programmatically.
    2) registering a custom scope programmatically (with a class that implements Scope interface)
    3) registering the definition of the proxy bean, assigning it the scope early registered and inserting it into the application context.

    In order to first step i've created a class (ProxyManager):



    Code:
    public class ProxyManager 
    {
    	private ProxyFactory pf ;
    	public Object getProxiedObject()
    	{
    		return pf.getProxy()==null?null:pf.getProxy();
    	}
    	
    	public Object registerProxy(String name, String className) 
    	{
    		Class<Object> classObj;
    		Object obj;
    		
    		try 
    		{
    			classObj = (Class<Object>) Class.forName(className);
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			
    			e.printStackTrace();
    			return null;
    		}
    			
    		try 
    		{
    			obj=classObj.newInstance();
    		} catch (InstantiationException e) {
    			
    			e.printStackTrace();
    			return null;
    		} catch (IllegalAccessException e) {
    			
    			e.printStackTrace();
    			return null;
    		}
    		pf = new ProxyFactory();					
    		pf.setTargetClass(obj.getClass());
    		//pf.setTarget(obj);		
    		return pf.getProxy();
    
    	}
    
    }
    The client code, in order to create the proxy programmatically is:

    Code:
    ProxyManager pm = new ProxyManager();
    pm.registerProxy("proxyName", "proxiedClass");
    The 2 step is:
    Code:
    CustomScope customScope = new CustomScope();				
    		((ConfigurableApplicationContext) appContext).getBeanFactory().registerScope("thread", customScope);
    the final step:

    Code:
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)((ConfigurableApplicationContext) appContext).getBeanFactory();
    BeanDefinition definition = new RootBeanDefinition(pm.getProxiedObject().getClass());
    definition.setScope("thread");
    beanFactory.registerBeanDefinition("threadScoped", definition);


    The abiove code works, but i would some feedback confirmation by experts.

    Best regards
Working...
X