-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDynamicProxyDemo.java
More file actions
33 lines (26 loc) · 1.11 KB
/
DynamicProxyDemo.java
File metadata and controls
33 lines (26 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
31
package proxy.dynamicProxy;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;
//动态代理模式
public class DynamicProxyDemo {
public static void main(String[] args) {
//1.创建目标对象
SubjectImpl realSubject = new SubjectImpl();
//2.创建调用处理器对象
ProxyHandler handler = new ProxyHandler(realSubject);
//3.动态生成代理对象
Subject proxySubject =
(Subject)Proxy.newProxyInstance
(SubjectImpl.class.getClassLoader(),
SubjectImpl.class.getInterfaces(), handler);
//proxySubject真实类型com.sun.proxy.$Proxy0
//proxySubject继承Proxy类,实现Subject接口
//newProxyInstance的第二个参数,就是指定代理对象的接口
//4.客户端通过代理对象调用方法
//本次调用将自动被代理处理器的invoke方法接收
proxySubject.request();
System.out.println(proxySubject.getClass().getName());
System.out.println(proxySubject.getClass().getSuperclass().getName());
}
}