zoukankan      html  css  js  c++  java
  • 【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 }
  • 相关阅读:
    那些年,加班搞过的需求...
    在基于nuxt的移动端页面中引用mint UI的popup组件之父子组件传值
    nuxt下运行项目时内存溢出(out of memory)的一种情况
    转载的
    VUE旅程(2)
    用windbg+sos找出程序中谁占用内存过高,谁占用CPU过高(转载)
    分块算法总结(持续更新ing)
    【MATLAB建模学习实录【三】】插值与拟合、数据处理
    【MATLAB建模学习实录【二】】非线性规划函数模型板子
    【MATLAB建模学习实录【一】】MATLAB求解线性规划模板
  • 原文地址:https://www.cnblogs.com/Tsing-Evan/p/8439536.html
Copyright © 2011-2022 走看看