forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyInvocationHandler.java
More file actions
30 lines (28 loc) · 1.11 KB
/
Copy pathMyInvocationHandler.java
File metadata and controls
30 lines (28 loc) · 1.11 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
package com.bruis.learnaop.testjdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MyInvocationHandler implements InvocationHandler {
//代理目标对象
private Object target;
public MyInvocationHandler(Object target) {
super();
this.target = target;
}
/**
* 执行目标对象的方法
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("---------------------------------方法purchase购买前,代理人吃回扣了----------------------------");
//执行目标对象的方法
Object result = method.invoke(target, args);
System.out.println("----------------------------------方法purchase购买后,代理人跑了----------------------------------");
return result;
}
/**
* 获取目标对象的代理对象
*/
public Object getProxy() {
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this);
}
}