-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProxyHandler.java
More file actions
39 lines (35 loc) · 1.16 KB
/
ProxyHandler.java
File metadata and controls
39 lines (35 loc) · 1.16 KB
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
package proxy.AOPByDynamicProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
public class ProxyHandler implements InvocationHandler {
static String beforeMethod = "";
static String afterMethod = "";
private Person receiverObject;
public ProxyHandler(Person object){
this.receiverObject = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//处理before方法
if(beforeMethod!=null&&beforeMethod.length()>0){
ClassLoader cl = ProxyHandler.class.getClassLoader();
Class<?> c = cl.loadClass(receiverObject.getClass().getName());
Method m=c.getMethod(beforeMethod);
Object obj = c.newInstance();
m.invoke(obj);
}
//处理目标方法
Object result = method.invoke(receiverObject, args);
//处理after方法
if(afterMethod!=null&&afterMethod.length()>0){
method.invoke(receiverObject, args);
ClassLoader cl = ProxyHandler.class.getClassLoader();
Class<?> c = cl.loadClass(receiverObject.getClass().getName());
Method m=c.getMethod(afterMethod);
Object obj = c.newInstance();
m.invoke(obj);
}
return result;
}
}