【Java SE】动态代理

 1 import java.lang.reflect.InvocationHandler;
 2 import java.lang.reflect.Method;
 3 import java.lang.reflect.Proxy;
 4 
 5 public class SimpleDynamicProxy {
 6 //    public static void consumer(Interface2 iface){
 7 //        iface.doSth2();
 8 //        iface.sthElse2("boboboo");
 9 //    }
10     public static void main(String[] args) {
11         //1
12         RealObject ro = new RealObject();
13 //        consumer(ro);
14         //2
15         Interface2 proxy =
16                 (Interface2) Proxy.newProxyInstance
17                     (ro.getClass().getClassLoader(),
18                     new Class[]{Interface.class, Interface2.class},
19                     new DynamicProxyHandle(ro));
20 //        consumer(proxy);
21         proxy.doSth2();
22         proxy.sthElse2("hello world!");
23         
24         Interface2 proxy2 =
25                 (Interface2) Proxy.newProxyInstance
26                     (ro.getClass().getClassLoader(),
27                     new Class[]{Interface2.class},//如果这个参数不同,handle.invoke()会识别为两不同的proxy($Proxy0,$Proxy1)
28 //                    new Class[]{Interface.class, Interface2.class},
29                     new DynamicProxyHandle(ro));
30         proxy2.doSth2();
31         ro.setStr("better world!");//有影响
32         ro = null;//无影响    因为将引用置空并不能影响对象本身的状态??
33         proxy2.doSth2();
34         proxy2.sthElse2("hello world!");
35     }
36 }
37 class DynamicProxyHandle implements InvocationHandler{
38     private Object proxied;//被代理对象,不表达对象   此例即  RealObject
39     public DynamicProxyHandle(Object proxied){
40         this.proxied = proxied;
41     }
42     @Override//为什么要传proxy?针对不同proxy可能会有不同处理
43     public Object invoke(
44             Object proxy, Method method, Object[] args//args
45             ) throws Throwable {
46         System.out.println(proxy.getClass().getSimpleName());
47         /*!在这里进行更多操作*/
48 //        System.out.println("do More Before");
49         String ret = (String) method.invoke(proxied,args);
50         System.out.println("return -> "+ret);
51 //        System.out.println("do More After");
52         return null;
53     }
54     
55 }
56 
57 interface Interface{
58     String doSth();
59     void sthElse(String arg);
60 }
61 interface Interface2{
62     String doSth2();
63     void sthElse2(String arg);
64 }
65 class RealObject implements Interface,Interface2{
66 
67     private String str = "hello world!";
68     public void setStr(String str){
69         this.str = str;
70     }
71     @Override
72     public String doSth() {
73         // TODO Auto-generated method stub
74         System.out.println("do sth "+str);
75         return "return obj";
76     }
77 
78     @Override
79     public void sthElse(String arg) {
80         // TODO Auto-generated method stub
81         System.out.println("do else "+arg);
82     }
83 
84     @Override
85     public String doSth2() {
86         // TODO Auto-generated method stub
87         System.out.println("do sth2 "+str);
88         return "return obj2";
89     }
90 
91     @Override
92     public void sthElse2(String arg) {
93         // TODO Auto-generated method stub
94         System.out.println("do else2 "+arg);
95     }
96     
97 }
原文地址:https://www.cnblogs.com/Tsing-Evan/p/8439536.html