-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGlibAgent.java
More file actions
33 lines (27 loc) · 975 Bytes
/
CGlibAgent.java
File metadata and controls
33 lines (27 loc) · 975 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
package common_problem;
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 CGlibAgent implements MethodInterceptor {
private Object proxy;
public static void main(String[] args) {
CGlibAgent cGlibAgent = new CGlibAgent();
Dog dog = (Dog) cGlibAgent.getInstance(new Dog());
dog.run();
}
public Object getInstance(Object proxy) {
this.proxy = proxy;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(proxy.getClass());
//创建回调对象
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("日志织入");
Object res = methodProxy.invokeSuper(o, objects);
return res;
}
}