zoukankan      html  css  js  c++  java
  • java动态代理例子


     1
     先看一个简单点动态代理的例子(没有用到factory)
     2 1 定义接口
     3 package com.dynamic.simple;
     4 
     5 import com.dynamic.simple.Subject;
     6 
     7 public interface Subject{   
     8     public void request();   
     9 }  
    10 
    11 2 实现真实类
    12 package com.dynamic.simple;
    13 
    14 public class RealSubject implements Subject{    
    15     public RealSubject(){}    
    16     public void request() {
    17         System.out.println("From real subject.");    
    18     }    
    19 }   
    20 
    21 3 定义动态代理
    22 package com.dynamic.simple;
    23 
    24 import java.lang.reflect.Method;    
    25 import java.lang.reflect.InvocationHandler;    
    26 public class DynamicSubject implements InvocationHandler {    
    27     private Object sub;    
    28     
    29     public DynamicSubject() {    
    30     }    
    31     
    32     public DynamicSubject(Object obj) {    
    33         sub = obj;    
    34     }    
    35     
    36     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {    
    37         System.out.println("before calling " + method);    
    38         method.invoke(sub,args);    
    39         System.out.println("after calling " + method);    
    40         return null;    
    41     }    
    42 }  
    43 
    44 4 客户端
    45 package com.dynamic.simple;
    46 
    47 import java.lang.reflect.InvocationHandler;    
    48 import java.lang.reflect.Proxy;    
    49 
    50 public class Client{    
    51     static public void main(String[] args) throws Throwable{    
    52         RealSubject rs = new RealSubject(); //在这里指定被代理类    
    53         InvocationHandler ds = new DynamicSubject(rs); //初始化代理类    
    54         Class cls = rs.getClass();    
    55         Subject subject = (Subject) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(),ds );    
    56         subject.request();    
    57     }   
    58 }

    运行客户端结果为:

    before calling public abstract void com.dynamic.simple.Subject.request()
    From real subject.
    after calling public abstract void com.dynamic.simple.Subject.request()

      1 下面看一个复杂点的动态代理实现(用到了factory)
      2 1 定义接口
      3 package com.proxy;
      4 
      5 import com.proxy.SomeClass;
      6 import com.proxy.SomeClassImpl;
      7 
      8 public interface SomeClass {
      9     public void someMethod();
     10     
     11     public void someOtherMethod(final String text);
     12 }
     13 
     14 2 定义实现类
     15 package com.proxy;
     16 
     17 
     18 public class SomeClassImpl implements SomeClass{
     19     private String name;
     20     
     21     public SomeClassImpl(final String name){
     22         this.name = name;
     23     }
     24     
     25     public void someMethod(){
     26         System.out.println(this.name);
     27     }
     28     
     29     public void someOtherMethod(final String text){
     30         System.out.println(text);
     31     }
     32 }
     33 
     34 3 定义2个代理SomeClassProxy和SomeClassCountingProxy(静态代理时才需要)
     35 /*SomeClassProxy*/
     36 package com.proxy;
     37 
     38 
     39 public class SomeClassProxy implements SomeClass{
     40     
     41     private final SomeClassImpl impl;
     42     
     43     public SomeClassProxy(SomeClassImpl impl){
     44         this.impl = impl;
     45     }
     46     
     47     public void someMethod(){
     48         this.impl.someMethod();
     49     }
     50     
     51     public void someOtherMethod(final String text){
     52         this.impl.someOtherMethod(text);
     53     }
     54 }
     55 
     56 /*SomeClassCountingProxy*/
     57 package com.proxy;
     58 
     59 
     60 public class SomeClassCountingProxy implements SomeClass{
     61     private final SomeClassImpl impl;
     62     
     63     private int invocationCount = 0;
     64     
     65     public SomeClassCountingProxy(SomeClassImpl impl){
     66         this.impl = impl;
     67     }
     68     
     69     public void someMethod(){
     70         this.invocationCount ++;
     71         this.impl.someMethod();
     72     }
     73     
     74     public void someOtherMethod(final String text){
     75         this.invocationCount ++;
     76         this.impl.someOtherMethod(text);
     77     }
     78 
     79     public int getInvocationCount() {
     80         return invocationCount;
     81     }
     82 
     83     public void setInvocationCount(int invocationCount) {
     84         this.invocationCount = invocationCount;
     85     }
     86 }
     87 
     88 4 定义工厂
     89 package com.proxy;
     90 
     91 import java.lang.reflect.InvocationHandler;
     92 import java.lang.reflect.Proxy;
     93 
     94 
     95 public class SomeClassFactory {
     96     /*//static proxy
     97     public static final SomeClass getStaticSomeClassProxy(){
     98         SomeClassImpl impl = new SomeClassImpl(System.getProperty("user.name"));
     99         if (false) {//generally when debuging
    100             return new SomeClassCountingProxy(impl);
    101         } else {
    102             return new SomeClassProxy(impl);
    103         }
    104     }
    105     */
    106     
    107     //dynamic proxy
    108     public static final SomeClass getDynamicSomeClassProxy(){
    109         SomeClassImpl impl = new SomeClassImpl(System.getProperty("user.name"));
    110         ClassLoader loader = SomeClassImpl.class.getClassLoader();
    111         Class[] interfaces = new Class[] {SomeClass.class};
    112         InvocationHandler handler = new MethodCountingHandler(impl);
    113         SomeClass proxy = (SomeClass)Proxy.newProxyInstance(loader, interfaces, handler);
    114         return proxy;
    115     }
    116 }
    117 
    118 5 定义InvocationHandler,主要通过这个类实现动态代理功能及其扩展
    119 package com.proxy;
    120 
    121 import java.lang.reflect.InvocationTargetException;
    122 import java.lang.reflect.InvocationHandler;
    123 import java.lang.reflect.Method;
    124 
    125 
    126 public class MethodCountingHandler implements InvocationHandler{
    127 
    128     private final Object impl;
    129     private int invocationCount = 0;
    130     
    131     public MethodCountingHandler(final Object impl){
    132         this.impl = impl;
    133     }
    134     
    135     public int getInvocationCount(){
    136         return invocationCount;
    137     }
    138     
    139     @Override
    140     public Object invoke(Object proxy, Method method, Object[] args)
    141             throws Throwable {
    142         try {
    143             this.invocationCount++;
    144             Object result = method.invoke(impl, args);
    145             return result;
    146         } catch (final InvocationTargetException e) {
    147             throw e.getTargetException();
    148         }
    149     }
    150 }
    151 
    152 
    153 6 定义客户端
    154 package com.proxy;
    155 
    156 import java.lang.reflect.InvocationHandler;
    157 import java.lang.reflect.Proxy;
    158 
    159 
    160 public class DemoDynamic {
    161 
    162     /**
    163      * @param args
    164      */
    165     public static void main(String[] args) {
    166         /*//no use factory
    167         SomeClassImpl real = new SomeClassImpl(System.getProperty("user.name"));
    168         InvocationHandler dynamic = new MethodCountingHandler(real);
    169         Class clz = real.getClass();
    170         SomeClass obj = (SomeClass)Proxy.newProxyInstance(clz.getClassLoader(), clz.getInterfaces(), dynamic);
    171         obj.someMethod();
    172         obj.someOtherMethod("test");
    173         */
    174         
    175         //use factory to get a real object
    176         SomeClass obj = (SomeClass)SomeClassFactory.getDynamicSomeClassProxy();
    177         obj.someMethod();
    178         obj.someOtherMethod("test");
    179         
    180         //the invocationhandler works as proxy function, extend the function of real object
    181         InvocationHandler handler = Proxy.getInvocationHandler(obj);
    182         if (handler instanceof MethodCountingHandler) {
    183             System.out.println(((MethodCountingHandler)handler).getInvocationCount());
    184         }
    185     }
    186 }

    运行结果为:

    Administrator
    test
    2

    参照链接:

    http://blog.csdn.net/goodluckforlove/archive/2009/10/13/4663079.aspx 

  • 相关阅读:
    ZK简单操作
    Java 混沌实验执行器 chaosblade-exec-jvm
    数仓分层
    维护大数据系统时遇到的几个问题及解决方案
    hotkey
    C# 构建一个TCP和SerialPort通信的通用类(下)
    C# 构建一个TCP和SerialPort通信的通用类(上)
    Prism框架中的DelagateCommand(下)
    Prism框架中的DelagateCommand(上)
    如何搭建自己的Nuget服务器(上)
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1970899.html
Copyright © 2011-2022 走看看