java动态代理-InvocationHandler Proxy

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:256713394; mso-list-template-ids:-1699456634;} @list l1 {mso-list-id:292448747; mso-list-template-ids:1567683388;} @list l2 {mso-list-id:1199665814; mso-list-template-ids:1173917332;} @list l3 {mso-list-id:1333723713; mso-list-template-ids:1064840542;} @list l4 {mso-list-id:2066417278; mso-list-template-ids:-1552372328;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

实现动态代理:

真实的接口:

Java 代码

  1. public interface Hello {  
  2.   
  3.     void sayHello(String to);  
  4.     
  5.     void print(String p);   
  6.    
  7. }  

public interface Hello {

 

    void sayHello(String to);

 

    void print(String p);

 

}


它的真实实现类:

Java 代码

  1. public class HelloImpl implements Hello {  
  2.       
  3.     public void sayHello(String to) {  
  4.         System.out.println("Say hello to " + to);  
  5.     }  
  6.       
  7.     public void print(String s) {  
  8.         System.out.println("print : " + s);  
  9.     }  
  10.       
  11. }  

public class HelloImpl implements Hello {

   

    public void sayHello(String to) {

        System.out.println("Say hello to " + to);

    }

   

    public void print(String s) {

        System.out.println("print : " + s);

    }

   

}


在这里生成与代理类相关联的InvocationHandler 对象

Java 代码

  1. public class LogHandler implements InvocationHandler {  
  2.       
  3.     private Object dele;  
  4.       
  5.     public LogHandler(Object obj) {  
  6.         this.dele = obj;  
  7.     }  
  8.       
  9.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  10.         doBefore();  
  11.         // 在这里完全可以把下面这句注释掉,而做一些其它的事情  
  12.         Object result = method.invoke(dele, args);  
  13.         after();  
  14.         return result;  
  15.     }  
  16.       
  17.     private void doBefore() {  
  18.         System.out.println("before....");  
  19.     }  
  20.       
  21.     private void after() {  
  22.         System.out.println("after....");  
  23.     }  
  24. }  

public class LogHandler implements InvocationHandler {

   

    private Object dele;

   

    public LogHandler(Object obj) {

        this.dele = obj;

    }

   

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        doBefore();

        // 在这里完全可以把下面这句注释掉,而做一些其它的事情

        Object result = method.invoke(dele, args);

        after();

        return result;

    }

   

    private void doBefore() {

        System.out.println("before....");

    }

   

    private void after() {

        System.out.println("after....");

    }

}


最后是测试类:

Java 代码

  1. public class ProxyTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         HelloImpl impl = new HelloImpl();  
  5.         LogHandler handler = new LogHandler(impl);  
  6.         // 这里把handlerimpl 新生成的代理类相关联  
  7.         Hello hello = (Hello) Proxy.newProxyInstance(impl.getClass().getClassLoader(), impl.getClass().getInterfaces(), handler);  
  8.           
  9.         // 这里无论访问哪个方法,都是会把请求转发到handler.invoke  
  10.         hello.print("All the test");  
  11.         hello.sayHello("Denny");  
  12.     }  
  13.   
  14. }  

public class ProxyTest {

 

    public static void main(String[] args) {

        HelloImpl impl = new HelloImpl();

        LogHandler handler = new LogHandler(impl);

        // 这里把handlerimpl 新生成的代理类相关联

        Hello hello = (Hello) Proxy.newProxyInstance(impl.getClass().getClassLoader(), impl.getClass().getInterfaces(), handler);

       

        // 这里无论访问哪个方法,都是会把请求转发到handler.invoke

        hello.print("All the test");

        hello.sayHello("Denny");

    }

 

}


这里是输出结果:

Java 代码

  1. before....  
  2. print : All the test  
  3. after....  
  4. before....  
  5. Say hello to Denny  
  6. after....  

before....

print : All the test

after....

before....

Say hello to Denny

after....

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值