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
| import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* 被代理类
*/
public class Teacher
{
public String teach()
{
System.err.println("Teacher is running");
return "嘿嘿";
}
/**
* 无法使用对象调用static方法
*/
// public static String teach2()
// {
// System.err.println(" static Teacher is running");
// return "static";
// }
/**
* final 无法被代理
* @return
*/
// public final String teach3()
// {
// System.err.println(" final Teacher is running");
// return "final";
// }
}
/**
* cglib 代理
*/
class ProxyFactory implements MethodInterceptor
{
//维护一个目标对象
Object target;
//构造器传入目标对象
public ProxyFactory(Object target)
{
this.target = target;
}
//返回一个代理对象 target对象的代理对象
public Object getProxyInstance()
{
//1. 创建一个工具类
Enhancer enhancer = new Enhancer();
//2. 设置父类
enhancer.setSuperclass(target.getClass());
//3. 设置回调函数
enhancer.setCallback(this);
//4. 创建子类对象,即代理对象
return enhancer.create();
}
/**
*
* @param o 被代理类
* @param method 被代理的方法,
* @param objects 被代理的方法的参数
* @param methodProxy
* @return 返回的是代理模式修饰后的被代理方法返回值
* @throws Throwable
* 重写intercept方法,会调用目标对象的方法
*/
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable
{
System.err.println("cglib代理模式--前");
Object invoke = method.invoke(target, objects);
System.err.println("cglib代理模式--后");
return invoke;
}
}
/**
* proxy 代理模式
*/
public class Client
{
public static void main(String[] args)
{
Teacher teacher = new Teacher();
ProxyFactory proxyFactory = new ProxyFactory(teacher);
Teacher proxyInstance = (Teacher) proxyFactory.getProxyInstance();
// System.err.println(proxyInstance);
System.err.println(proxyInstance.getClass());
System.err.println(proxyInstance.teach());
// System.err.println(proxyInstance.teach2());
// System.err.println(proxyInstance.teach3());
}
}
|