1.jdk動態代理
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
|
/** * */ package com.sinosoft; /** *接口:編寫一個委托類的接口,即靜態代理的(Apple接口) * */ public interface Apple { public void phoneCall(); } /** * */ package com.sinosoft; /** * 實現一個真正的委托類,即靜態代理的(AppleImpl類) * */ public class AppleImpl implements Apple { /* * 打電話 */ @Override public void phoneCall() { System.out.println("打電話"); } } /** * */ package com.sinosoft; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; /** * 創建一個動態代理類,實現InvocationHandler接口,并重寫該invoke方法 * */ public class DynamicProxy implements InvocationHandler{ private Object object; public DynamicProxy(Object object) { this.object=object; } /* * proxy參數傳遞的即是代理類的實例。method是調用的方法,即需要執行的方法;args是方法的參數; * @param proxy * @param method * @param args * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = method.invoke(object, args); return result; } } /** * */ package com.sinosoft; import java.lang.reflect.Proxy; /** * @author jdk動態代理 * */ public class testDynamicProxy { public static void main(String[] args) { //1.創建接口的實現類 Apple tApple = new AppleImpl(); //2.動態代理類 DynamicProxy tDynamicProxy = new DynamicProxy(tApple); ClassLoader tClassLoader = tApple.getClass().getClassLoader(); // 創建動態代理的對象,需要借助Proxy.newProxyInstance。該方法的三個參數分別是: // ClassLoader loader表示當前使用到的appClassloader。 // Class<?>[] interfaces表示目標對象實現的一組接口。 // InvocationHandler h表示當前的InvocationHandler實現實例對象。 Apple apple = (Apple) Proxy.newProxyInstance(tClassLoader, new Class[] { Apple. class }, tDynamicProxy); apple.phoneCall(); } } |
2.cglib動態代理
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
|
/** * */ package com.sinosoft; /** * 實現一個真正的委托類,即靜態代理的(AppleImpl類) * */ public class AppleClass{ /* * 打電話 */ public void phoneCall() { System.out.println("打電話"); } } /** * */ package com.sinosoft; import java.lang.reflect.Method; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; /** * @author Administrator * */ public class CglibProxy implements MethodInterceptor{ /* * 方法功能描述 * @param obj * @param method * @param args * @param proxy * @return * @throws Throwable * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy) */ @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { // TODO Auto-generated method stub Object object= proxy.invokeSuper(obj, args); return object; } } /** * */ package com.sinosoft; import net.sf.cglib.proxy.Enhancer; /** * @author Administrator * */ public class TestCglibProxy { public static void main(String[] args) { CglibProxy tCglibProxy= new CglibProxy(); Enhancer tEnhancer= new Enhancer(); tEnhancer.setSuperclass(AppleClass. class ); tEnhancer.setCallback(tCglibProxy); AppleClass tApple= (AppleClass)tEnhancer.create(); tApple.phoneCall(); } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/u014401141/article/details/70312643