创建动态代理服务

来源:互联网 发布:mac画流程图软件 编辑:程序博客网 时间:2024/04/30 16:44
Java 提供了强大的动态能力,在客户端可以只知道接口来调用本地或者远程的服务。

在客户端只关心接口是什么:
/**
 *DynamicProxyClient.java
 *2007-4-13
 
*/

package com.kompakar.tut.proxy.dynamic;

/**
 * <p>Project: proxyExample</p>
 * <p>Title:  DynamicProxyClient</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright   2007.All rights reserved.</p>
 * 
@author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
 * 
@version 1.0
 * 
@see 
 
*/


public class DynamicProxyClient {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
         
        MyBusiness server 
= BusinessServiceFactory.getDynamicBusinessProxy(MyBusiness.class);
        System.out.println(server.echo(
"Alan!"));

    }


}


 


服务工厂返回一个服务:
/**
 *BusinessFactory.java
 *2007-4-13
 
*/

package com.kompakar.tut.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
 
/**
 * <p>Project: proxyExample</p>
 * <p>Title:  BusinessFactory</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright   2007.All rights reserved.</p>
 * 
@author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
 * 
@version 1.0
 * 
@see 
 
*/


public class BusinessServiceFactory {

      
/**
       * Gets proxy to depending upon debug status in Log4J.
       * 
       * 
@return The proxy to the implementation.
       
*/

      
public static final <T> T  getDynamicBusinessProxy(Class<T>   serviceClass) {
          InvocationHandler handler 
= new DynamicBusinessServiceHandler(serviceClass);
        Class[] interfaces 
= new Class[] { serviceClass };
        ClassLoader loader 
= BusinessServiceFactory.class.getClassLoader();
        
return (T) Proxy.newProxyInstance(loader, interfaces, handler);
      }

}


 

DynamicBusinessServiceHandler是代理的关键,他可以拦截服务调用的方法,并且完成需要做的任务。
/**
 *DynamicBusinessHandler.java
 *2007-4-13
 
*/

package com.kompakar.tut.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * <p>Project: proxyExample</p>
 * <p>Title:  DynamicBusinessHandler</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright   2007.All rights reserved.</p>
 * 
@author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
 * 
@version 1.0
 * 
@see 
 
*/


public class DynamicBusinessServiceHandler implements InvocationHandler {
    
     
private Class className;
     
      
public DynamicBusinessServiceHandler(Class name) {
          
this.className = name;
    }

      
      
/**
       * You can invoke any service as you wish,for exmaple: local object or a remote service by interface name;
       
*/

    
public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable {
         
        
try {
            Object impl 
= null;
            impl 
= getServiceByInterfaceName(className);
            Object result 
= meth.invoke(impl, args);
            
return result;
          }
 catch (final InvocationTargetException ex) {
            
throw ex.getTargetException();
          }
 
    }


    
/**
     * 
@param interfaceName
     * 
@return
     * 
@throws ClassNotFoundException
     * 
@throws InstantiationException
     * 
@throws IllegalAccessException
     
*/

    
private Object getServiceByInterfaceName(Class interfaceName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String interfaceString 
= interfaceName.getName();
        String className 
= interfaceString + "Impl";
        ClassLoader loader 
= DynamicBusinessServiceHandler.class.getClassLoader();
        Class serviceClass 
= Class.forName(className, false, loader);
        
return serviceClass.newInstance();
    }


}


 


当然自定义的接口和实现不可少
/**
 *IMyBusiness.java
 *2007-4-13
 
*/

package com.kompakar.tut.proxy.dynamic;

/**
 * <p>Project: proxyExample</p>
 * <p>Title:  IMyBusiness</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright   2007.All rights reserved.</p>
 * 
@author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
 * 
@version 1.0
 * 
@see 
 
*/


public interface MyBusiness {
    
    
public abstract String echo(String name);
}


 

/**
 *MyBusinessImple.java
 *2007-4-13
 
*/

package com.kompakar.tut.proxy.dynamic;

/**
 * <p>Project: proxyExample</p>
 * <p>Title:  MyBusinessImple</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright   2007.All rights reserved.</p>
 * 
@author <a href="mailto:benewu(at)gmail.com">Wu Xuefeng</a>
 * 
@version 1.0
 * 
@see 
 
*/


public class MyBusinessImpl implements MyBusiness{

    
public String echo(String name) {
         
        
return "hello, " + name ;
    }


}


 
原创粉丝点击