-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCglibProxy.java
More file actions
41 lines (33 loc) · 1002 Bytes
/
Copy pathCglibProxy.java
File metadata and controls
41 lines (33 loc) · 1002 Bytes
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
package proxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* Created by helmeter on 4/16/16.
*/
public class CglibProxy implements MethodInterceptor {
//private Object target;
/**
* 创建代理对象
*
* @param target
* @return
*/
private Enhancer enhancer = new Enhancer();
public Object getInstance(Class target) {
// this.target = target;
enhancer.setSuperclass(target);
// 回调方法
enhancer.setCallback(this);
// 创建代理对象
return enhancer.create();
}
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
Object result = null;
System.out.println("事物开始");
result = methodProxy.invokeSuper(o, objects);
System.out.println("事物结束");
return result;
}
}