java动态代理应用于RMI

动态代理是指客户通过代理类来调用其它对象的方法
动态代理使用场合:
•远程方法调用(RMI)
• 
1.创建一个实现接口InvocationHandler的类,它必须实现invoke方法
2.创建被代理的类以及接口
3.通过Proxy的静态方法
newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h) 创建一个代理
4.通过代理调用方法
 
参见程序VectorProxy.java
 
view plaincopy to clipboardprint?
package com.langsin.dynamicproxy;  
 
import java.lang.reflect.InvocationHandler;  
import java.lang.reflect.Method;  
import java.lang.reflect.Proxy;  
import java.util.List;  
import java.util.Vector;  
 
public class VectorProxy implements InvocationHandler  
{  
    private Object proxyobj;  
 
    public VectorProxy(Object obj)  
    {  
        proxyobj = obj;  
    }  
 
    public static Object factory(Object obj)  
    {  
        Class<?> cls = obj.getClass();  
 
        return Proxy.newProxyInstance(cls.getClassLoader(),  
                cls.getInterfaces(), new VectorProxy(obj));  
    }  
 
    public Object invoke(Object proxy, Method method, Object[] args)  
            throws Throwable  
    {  
        System.out.println("before calling " + method);  
 
        if (args != null)  
        {  
            for (int i = 0; i < args.length; i++)  
            {  
                System.out.println(args[i] + "");  
            }  
        }  
        Object object = method.invoke(proxyobj, args);  
 
        System.out.println("after calling " + method);  
        return object;  
    }  
 
    @SuppressWarnings("unchecked")  
    public static void main(String[] args)  
    {  
        List<String> v = (List<String>) factory(new Vector<String>(10));  
 
        v.add("New");  
        v.add("York");  
        System.out.println(v);  
 
        v.remove(0);  
        System.out.println(v);  
    }  

package com.langsin.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;

public class VectorProxy implements InvocationHandler
{
 private Object proxyobj;

 public VectorProxy(Object obj)
 {
  proxyobj = obj;
 }

 public static Object factory(Object obj)
 {
  Class<?> cls = obj.getClass();

  return Proxy.newProxyInstance(cls.getClassLoader(),
    cls.getInterfaces(), new VectorProxy(obj));
 }

 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable
 {
  System.out.println("before calling " + method);

  if (args != null)
  {
   for (int i = 0; i < args.length; i++)
   {
    System.out.println(args[i] + "");
   }
  }
  Object object = method.invoke(proxyobj, args);

  System.out.println("after calling " + method);
  return object;
 }

 @SuppressWarnings("unchecked")
 public static void main(String[] args)
 {
  List<String> v = (List<String>) factory(new Vector<String>(10));

  v.add("New");
  v.add("York");
  System.out.println(v);

  v.remove(0);
  System.out.println(v);
 }
}
 
参见程序Foo.java
参见程序FooImpl.java
参见程序FooImpl2.java
参见程序CommonInvocationHandler.java
参见程序Demo.java
view plaincopy to clipboardprint?
package com.langsin.dynamicproxy;  
 
public interface Foo  
{  
    void doAction();  
}  
 
 
package com.langsin.dynamicproxy;  
 
public class FooImpl implements Foo  
{  
    public FooImpl()  
    {  
    }  
 
    public void doAction()  
    {  
        System.out.println("in FooImp1.doAction()");  
    }  
}  
 
 
 
package com.langsin.dynamicproxy;  
 
public class FooImpl2 implements Foo  
{  
    public FooImpl2()  
    {  
    }  
 
    public void doAction()  
    {  
        System.out.println("in FooImp2.doAction()");  
    }  
 
}  
 
 
 
 
package com.langsin.dynamicproxy;  
 
import java.lang.reflect.InvocationHandler;  
import java.lang.reflect.Method;  
 
public class CommonInvocationHandler implements InvocationHandler  
{  
 
    // 动态执行对象,需要回调的对象  
    private Object target;  
 
    // 支持构造子注射  
    public CommonInvocationHandler()  
    {  
 
    }  
 
    // 支持构造子注射  
    public CommonInvocationHandler(Object target)  
    {  
        setTarget(target);  
    }  
 
    /** 
     *  
     * 采用setter方法注射 
     *  
     * @param target 
     *  
     */ 
    public void setTarget(Object target)  
    {  
        this.target = target;  
    }  
 
    /** 
     *  
     * 调用proxy中指定的方法method,并传入参数列表args 
     *  
     * @param proxy 
     *            代理类的类型,例如定义对应method的代理接口 
     *  
     * @param method 
     *            被代理的方法 
     *  
     * @param args 
     *            调用被代理方法的参数 
     *  
     * @return 
     *  
     * @throws java.lang.Throwable 
     *  
     */ 
 
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  
    {  
        return method.invoke(target, args);  
    }  
 
}  
 
 
package com.langsin.dynamicproxy;  
 
import java.lang.reflect.Proxy;  
 
public class Demo  
{  
    public static void main(String[] args)  
    {  
 
        // 1.通用的动态代理实现  
 
        CommonInvocationHandler handler = new CommonInvocationHandler();  
 
        Foo f;  
 
        // 2.接口实现1  
 
        handler.setTarget(new FooImpl());  
 
        // 方法参数说明:代理类、代理类实现的接口列表、代理类的处理器  
 
        // 关联代理类、代理类中接口方法、处理器,当代理类中接口方法被调用时,会自动分发到处理器的invoke方法  
 
        // 如果代理类没有实现指定接口列表,会抛出非法参数异常  
 
        f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),  
 
        new Class[] { Foo.class },  
 
        handler);  
 
        f.doAction();  
 
        // 3.接口实现2  
 
        handler.setTarget(new FooImpl2());  
 
        f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),  
 
        new Class[] { Foo.class },  
 
        handler);  
 
        f.doAction();  
    }  

package com.langsin.dynamicproxy;

public interface Foo
{
    void doAction();
}


package com.langsin.dynamicproxy;

public class FooImpl implements Foo
{
    public FooImpl()
    {
    }

    public void doAction()
    {
        System.out.println("in FooImp1.doAction()");
    }
}

 

package com.langsin.dynamicproxy;

public class FooImpl2 implements Foo
{
    public FooImpl2()
    {
    }

    public void doAction()
    {
        System.out.println("in FooImp2.doAction()");
    }

}

 


package com.langsin.dynamicproxy;

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

public class CommonInvocationHandler implements InvocationHandler
{

    // 动态执行对象,需要回调的对象
    private Object target;

    // 支持构造子注射
    public CommonInvocationHandler()
    {

    }

    // 支持构造子注射
    public CommonInvocationHandler(Object target)
    {
        setTarget(target);
    }

    /**
     *
     * 采用setter方法注射
     *
     * @param target
     *
     */
    public void setTarget(Object target)
    {
        this.target = target;
    }

    /**
     *
     * 调用proxy中指定的方法method,并传入参数列表args
     *
     * @param proxy
     *            代理类的类型,例如定义对应method的代理接口
     *
     * @param method
     *            被代理的方法
     *
     * @param args
     *            调用被代理方法的参数
     *
     * @return
     *
     * @throws java.lang.Throwable
     *
     */

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
    {
        return method.invoke(target, args);
    }

}


package com.langsin.dynamicproxy;

import java.lang.reflect.Proxy;

public class Demo
{
 public static void main(String[] args)
 {

  // 1.通用的动态代理实现

  CommonInvocationHandler handler = new CommonInvocationHandler();

  Foo f;

  // 2.接口实现1

  handler.setTarget(new FooImpl());

  // 方法参数说明:代理类、代理类实现的接口列表、代理类的处理器

  // 关联代理类、代理类中接口方法、处理器,当代理类中接口方法被调用时,会自动分发到处理器的invoke方法

  // 如果代理类没有实现指定接口列表,会抛出非法参数异常

  f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),

  new Class[] { Foo.class },

  handler);

  f.doAction();

  // 3.接口实现2

  handler.setTarget(new FooImpl2());

  f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),

  new Class[] { Foo.class },

  handler);

  f.doAction();
 }
}
 
调试


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/dabizime/archive/2010/05/06/5563574.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值