forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoInvokerHandler.java
More file actions
53 lines (42 loc) · 1.47 KB
/
DemoInvokerHandler.java
File metadata and controls
53 lines (42 loc) · 1.47 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.learnjava.proxy.dynamicproxy;
import com.learnjava.proxy.staticproxy.RealSubject;
import com.learnjava.proxy.staticproxy.Subject;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author bruis
*
* jdk动态代理
*
*/
public class DemoInvokerHandler implements InvocationHandler {
// 真正的业务对象
private Object realSubject;
public DemoInvokerHandler(Object realSubject) {
this.realSubject = realSubject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("代理预处理操作");
// 调用真正的代理对象逻辑
Object result = method.invoke(realSubject, args);
System.out.println("代理后处理操作");
return result;
}
/**
* 创建代理对象并返回
* @return
*/
public Object getProxy() {
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
realSubject.getClass().getInterfaces(), this);
}
public static void main(String[] args) {
RealSubject realSubject = new RealSubject();
DemoInvokerHandler invokerHandler = new DemoInvokerHandler(realSubject);
Subject proxy = (Subject) invokerHandler.getProxy();
// 拿到业务对象,执行业务逻辑,此时业务逻辑已经被代理对象代理了
proxy.operation();
}
}