-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
34 lines (28 loc) · 957 Bytes
/
Main.java
File metadata and controls
34 lines (28 loc) · 957 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
34
package proxy.AOPByDynamicProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Main {
public static void initXml(){
XmlReader.readXml("./src/proxy/AOPByDynamicProxy/aops.xml");
ResourceListener.addListener("./src/proxy/AOPByDynamicProxy");
}
public static void main(String[] args) throws Exception{
Main.initXml();
Person action = new PersonImpl(); //subject对象
ProxyHandler mh = new ProxyHandler(action); //代理处理器
ClassLoader cl = Main.class.getClassLoader();
Class<?> proxyClass = Proxy.getProxyClass(cl, new Class<?>[]{Person.class});//Proxy new了一个$proxy
Person proxy = (Person) proxyClass.getConstructor(new Class[]{InvocationHandler.class}).
newInstance(new Object[]{mh});//生成代理
while(true)
{
proxy.eat();
try{
Thread.sleep(3000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}