java动态代理

来源:互联网 发布:海量数据排序 编辑:程序博客网 时间:2024/06/03 20:59

http://www.importnew.com/26166.html


为了使代理类和被代理类对第三方有相同的函数,代理类和被代理类一般实现一个公共的interface,该interface定义如下

1
2
3
4
publicinterface Calculator {
    publicInteger add(Integer num1, Integer num2);
    publicInteger minus(Integer num1, Integer num2);
}

被代理类定义如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
publicclass CalculatorImpl implementsCalculator {
 
    @Override
    publicInteger add(Integer num1, Integer num2) {
        intret = num1 + num2;
        System.out.println("in calculatorImpl, res: " + ret);
        returnret;
    }
     
    @Override
    publicInteger minus(Integer num1, Integer num2) {
        intret = num1 - num2;
        System.out.println("int calculatorImpl, res: " + ret);
        returnret;
    }
 
}

代理需求:在add函数和minus函数调用前后分别输出before invocation和after invocation字样

静态代理解决方案

代码如下:简单直接,无需赘言,如果calculator里边不仅有add和minus,还有divide,product,log,sin…呢,呵呵哒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
publicclass StaticCalculatorProxy implementsCalculator {
    Calculator obj;
     
    publicStaticCalculatorProxy(Calculator obj) {
        this.obj = obj;
    }
 
    @Override
    publicInteger add(Integer num1, Integer num2) {
        System.out.println("in StaticCalculatorProxy, before invocation");
        Integer ret = obj.add(num1, num2);
        System.out.println("in StaticCalculatorProxy, after invocation");
        returnret;
    }
 
    @Override
    publicInteger minus(Integer num1, Integer num2) {
        System.out.println("in StaticCalculatorProxy, before invocation");
        Integer ret = obj.minus(num1, num2);
        System.out.println("in StaticCalculatorProxy, after invocation");
        returnret;
    }
 
}

动态代理解决方案

首先编写实现InvocationHandler接口的类,用于请求转发,实现如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
publicclass CalculatorHandler implementsInvocationHandler {
     
    privateObject obj; //被代理类
     
    publicCalculatorHandler(Object obj) {
        this.obj = obj;
    }
 
    @Override
    publicObject invoke(Object proxy, Method method, Object[] args) throwsThrowable {
        System.out.println("in calculatorhandler, before invocation");
         
        Object ret = method.invoke(obj, args);  //执行被代理类方法
         
        System.out.println("in calculationhandler, after invocation");
        returnret;
    }
 
}

生成动态代理

1
2
3
4
5
CalculatorImpl calculatorImpl = newCalculatorImpl();//被代理类
CalculatorHandler calculatorHandler = newCalculatorHandler(calculatorImpl);
Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler);
System.out.println(calculator.add(1,2));
System.out.println(calculator.minus(1,2));

无论calculator中包含多少函数,动态代理只需实现一次,实际工程中,System.out.println(“in calculatorhandler, before invocation”)可能是加缓存,打日志等操作

动态代理如何工作的

为了搞清楚动态代理如何工作,首先看看生成的动态代理的代码是什么,借助[1]中ProxyUtil代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
publicclass ProxyUtils {
 
    /**
     * Save proxy class to path
     *
     * @param path path to save proxy class
     * @param proxyClassName name of proxy class
     * @param interfaces interfaces of proxy class
     * @return
     */
    publicstatic boolean saveProxyClass(String path, String proxyClassName, Class[] interfaces) {
        if(proxyClassName == null|| path == null) {
            returnfalse;
        }
 
        // get byte of proxy class
        byte[] classFile = ProxyGenerator.generateProxyClass(proxyClassName, interfaces);
        FileOutputStream out = null;
        try{
            out = newFileOutputStream(path);
            out.write(classFile);
            out.flush();
            returntrue;
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            try{
                out.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
        returnfalse;
    }
}

得到了生成的动态代理代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
publicfinal class $Proxy0 extendsProxy
    implementsCalculator
{
 
    public$Proxy0(InvocationHandler invocationhandler)
    {
        super(invocationhandler);
    }
 
    publicfinal boolean equals(Object obj)
    {
        try
        {
            return((Boolean)super.h.invoke(this, m1, newObject[] {
                obj
            })).booleanValue();
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            thrownew UndeclaredThrowableException(throwable);
        }
    }
 
    publicfinal String toString()
    {
        try
        {
            return(String)super.h.invoke(this, m2, null);
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            thrownew UndeclaredThrowableException(throwable);
        }
    }
 
    publicfinal Integer minus(Integer integer, Integer integer1)
    {
        try
        {
            return(Integer)super.h.invoke(this, m4, newObject[] {
                integer, integer1
            });
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            thrownew UndeclaredThrowableException(throwable);
        }
    }
 
    publicfinal Integer add(Integer integer, Integer integer1)
    {
        try
        {
            return(Integer)super.h.invoke(this, m3, newObject[] {
                integer, integer1
            });
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            thrownew UndeclaredThrowableException(throwable);
        }
    }
 
    publicfinal int hashCode()
    {
        try
        {
            return((Integer)super.h.invoke(this, m0, null)).intValue();
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            thrownew UndeclaredThrowableException(throwable);
        }
    }
 
    privatestatic Method m1;
    privatestatic Method m2;
    privatestatic Method m4;
    privatestatic Method m3;
    privatestatic Method m0;
 
    static
    {
        try
        {
            m1 = Class.forName("java.lang.Object").getMethod("equals",newClass[] {
                Class.forName("java.lang.Object")
            });
            m2 = Class.forName("java.lang.Object").getMethod("toString",newClass[0]);
            m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus",newClass[] {
                Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
            });
            m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add",newClass[] {
                Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
            });
            m0 = Class.forName("java.lang.Object").getMethod("hashCode",newClass[0]);
        }
        catch(NoSuchMethodException nosuchmethodexception)
        {
            thrownew NoSuchMethodError(nosuchmethodexception.getMessage());
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            thrownew NoClassDefFoundError(classnotfoundexception.getMessage());
        }
    }
}

有点长,按照初始化顺序慢慢来分析,首先分析静态代码块:

1
2
3
4
5
6
7
8
9
10
11
m1 = Class.forName("java.lang.Object").getMethod("equals",newClass[] {
                    Class.forName("java.lang.Object")
                });
m2 = Class.forName("java.lang.Object").getMethod("toString",newClass[0]);
m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus",newClass[] {
                    Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
                });
m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add",newClass[] {
                    Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
                });
m0 = Class.forName("java.lang.Object").getMethod("hashCode",newClass[0]);

得到公共interface中的add函数和minus函数对应的Method方法,同事也得到了equals,toString,hashCode三个函数的Method,所以调用代理类的equals,toString,hashCode也是要执行被代理类的方法的,知道这点很有必要

构造函数

1
2
3
4
public$Proxy0(InvocationHandler invocationhandler)
{
        super(invocationhandler);
}

初始化了内部的InvocationHandler变量,也就是下文的super.h

以add为例看一下请求的转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
publicfinal Integer add(Integer integer, Integer integer1)
{
    try
    {
        return(Integer)super.h.invoke(this, m3, newObject[] {
            integer, integer1
        });
    }
    catch(Error _ex) { }
    catch(Throwable throwable)
    {
        thrownew UndeclaredThrowableException(throwable);
    }
}

super.h.invoke就是invocationhandler.invoke就是传入的CalculatorHandler中实现的

1
2
3
4
5
6
7
8
9
@Override
publicObject invoke(Object proxy, Method method, Object[] args) throwsThrowable {
    System.out.println("in calculatorhandler, before invocation");
     
    Object ret = method.invoke(obj, args);  //执行被代理类方法
     
    System.out.println("in calculationhandler, after invocation");
    returnret;
}

最终执行的就是CalculatorHandler对应的invoke函数

总结

1
Calculator calculator = (Calculator) Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler);

生成动态代理的过程步骤如下[2]:

1
2
3
4
5
6
7
8
9
10
11
12
// InvocationHandlerImpl 实现了 InvocationHandler 接口,并能实现方法调用从代理类到委托类的分派转发
// 其内部通常包含指向委托类实例的引用,用于真正执行分派转发过来的方法调用
InvocationHandler handler = newInvocationHandlerImpl(..);
  
// 通过 Proxy 为包括 Interface 接口在内的一组接口动态创建代理类的类对象
Class clazz = Proxy.getProxyClass(classLoader, newClass[] { Interface.class, ... });
  
// 通过反射从生成的类对象获得构造函数对象
Constructor constructor = clazz.getConstructor(newClass[] { InvocationHandler.class});
  
// 通过构造函数对象创建动态代理类实例
Interface Proxy = (Interface)constructor.newInstance(newObject[] { handler });

Proxy.newProxyInstance帮我们做了2,3,4步,直接返回给我们一个动态代理对象,代理对象最终执行InvocationHandler中invoke函数。顺便强推文章[2]


原创粉丝点击