Java中动态代理类_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java中动态代理类

Java中动态代理类

 2018/10/17 12:45:45  andrew7676  程序员俱乐部  我要评论(0)
  • 摘要:1.Java中动态代理模式实现动态代理必须实现InvocationHandler接口,实现invoke()方法。生成代理使用Proxy类(代理)的newProxyInstance()方法。publicinterfaceSubject{publicvoidrequest();}publicclassRealSubjectimplementsSubject{publicvoidrequest(){System.out.println("Fromrealsubject!");}}importjava
  • 标签:Java 代理
1. Java中动态代理模式
class="java" name="code">
实现动态代理必须实现InvocationHandler接口,实现invoke()方法。
生成代理使用Proxy类(代理)的newProxyInstance()方法。

public interface Subject{
    public void request();
}

public class RealSubject implements Subject{
    public void request(){
        System.out.println("From real subject!");
    }
}

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
 * 该代理类的内部属性是Object类型,实际使用的时候通过该类的构造方法传递进来一个对象此外,该类还实现了invoke方法,该方法中的method.invoke其实就是调用被代理对象的将要执行的方法,方法参数是sub,表示该方法从属于sub,通过动态代理类,我们可以在执行真实对象的方法前后加入自己的一些额外方法。*/
public class DynamicSubject implements InvocationHandler{
    private Object sub;
    public DynamicSubject(Object obj){
        this.sub = obj;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
        System.out.println("before calling: " + method);
        method.invoke(sub, args);
        System.out.println(args == null);
        System.out.println("after calling: " + method);
        return null;
    }
}

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Client{
    public static void main(String[] args){
        RealSubject realSubject = new RealSubject();
        InvocationHandler handler = new DynamicSubject(realSubject);
        Class<?> classType = handler.getClass();
        // 下面的代码一次性生成代理
        Subject subject = (Subject) Proxy.newProxyInstance(classType
.getClassLoader(), realSubject.getClass().getInterfaces(),handler);
        subject.request();
        System.out.println(subject.getClass());
    }
}
运行结果:
before calling: public abstract void org09.Subject2.request()
From real subject2!
true
after calling: public abstract void org09.Subject2.request()
class $Proxy0


2. VectorProxy
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){
        this.proxyObj = obj;
    }
    public static Object factory(Object obj){
        Class<?> classType = obj.getClass();
        return Proxy.newProxyInstance(classType.getClassLoader(),
classType.getInterfaces(), new VectorProxy(obj));
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
        System.out.println("before calling: " + method);
        if(null != args){
            for(Object obj : args){
                System.out.println(obj);
            }
        }
        Object object = method.invoke(proxyObj, args);
        System.out.println("after calling: " + method);
        return object;
    }
    public static void main(String[] args)
    {
        List v = (List)factory(new Vector());
        System.out.println(v.getClass().getName());
        System.out.println("---------------1");
        v.add("New");
        System.out.println("---------------2");
        v.add("York");
        System.out.println("---------------3");
        System.out.println(v);
        System.out.println("---------------4");
        v.remove(0);
        System.out.println("---------------5");
        System.out.println(v);        
    }
}
运行结果:
$Proxy0
---------------1
before calling: public abstract boolean java.util.List.add(java.lang.Object)
New
after calling: public abstract boolean java.util.List.add(java.lang.Object)
---------------2
before calling: public abstract boolean java.util.List.add(java.lang.Object)
York
after calling: public abstract boolean java.util.List.add(java.lang.Object)
---------------3
before calling: public java.lang.String java.lang.Object.toString()
after calling: public java.lang.String java.lang.Object.toString()
[New, York]
---------------4
before calling: public abstract java.lang.Object java.util.List.remove(int)
0
after calling: public abstract java.lang.Object java.util.List.remove(int)
---------------5
before calling: public java.lang.String java.lang.Object.toString()
after calling: public java.lang.String java.lang.Object.toString()
[York]


3. 动态代理具体实现
public interface Foo{
    void doAction();
}

public class FooImpl implements Foo{
    public void doAction(){
        System.out.println("in FooImpl doAction!");
    }
}

public class FooImpl2 implements Foo{
    public void doAction(){
        System.out.println("in FooImpl2 doAction!");
    }
}

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class CommonInvocationHandler implements InvocationHandler{
    private Object target;
    public CommonInvocationHandler(Object target){
        this.target = target;
    }
    public CommonInvocationHandler(){
        
    }
    public void setTarget(Object target){
        this.target = target;
    }
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable{
        return method.invoke(target, args);
    }
}

import java.lang.reflect.Proxy;
public class Demo{
    public static void main(String[] args)    {
        CommonInvocationHandler handler = new CommonInvocationHandler();
        Foo f = null;
        handler.setTarget(new FooImpl());
        f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                new Class[] { Foo.class }, handler);
        f.doAction();
        System.out.println("-------------------");
        handler.setTarget(new FooImpl2());
        f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                new Class[] { Foo.class }, handler);
        f.doAction();
    }
}
运行结果:
in FooImpl doAction!
---------
in FooImpl2 doAction!
上一篇: Java中annotation注解 下一篇: 没有下一篇了!
发表评论
用户名: 匿名