- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGlibAgent.java
More file actions
Latest commit
33 lines (27 loc) · 975 Bytes
/
Copy pathCGlibAgent.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
packagecommon_problem;
importnet.sf.cglib.proxy.Enhancer;
importnet.sf.cglib.proxy.MethodInterceptor;
importnet.sf.cglib.proxy.MethodProxy;
importjava.lang.reflect.Method;
publicclassCGlibAgentimplementsMethodInterceptor {
privateObjectproxy;
publicstaticvoidmain(String[] args) {
CGlibAgentcGlibAgent = newCGlibAgent();
Dogdog = (Dog) cGlibAgent.getInstance(newDog());
dog.run();
}
publicObjectgetInstance(Objectproxy) {
this.proxy = proxy;
Enhancerenhancer = newEnhancer();
enhancer.setSuperclass(proxy.getClass());
//创建回调对象
enhancer.setCallback(this);
returnenhancer.create();
}
@Override
publicObjectintercept(Objecto, Methodmethod, Object[] objects, MethodProxymethodProxy) throwsThrowable {
System.out.println("日志织入");
Objectres = methodProxy.invokeSuper(o, objects);
returnres;
}
}