Design mode----Dynamic Proxy mode

Source: Internet
Author: User

Before reading this article please read the previous article http://10572970.blog.51cto.com/10562970/1759600, the example of this article from the previous article.

     the first implementation of a dynamic proxy requires a interface provided by the JDK (invocationhandler ) and a class ( proxy ) to support. First say     invocationhandler this interface, inside there is a public   Object Invoke ( Object Proxy, method, object[] args) throws   throwable method, The method that the acting proxy class executes. This means that each invocation of a method by the proxy class triggers the Invoke method of the Invocationhandler associated with the proxy class. The parameter description for invoke:

Object proxy refers to objects that are being proxied

Methods for method Proxy invocation

Object[] args represents method parameters

The following is the proxy class, which provides some static methods to create the proxy class. OK, look at the code:

Myhandler<<span style= "color: #20999d;" >T> [Object Object]invocationhandler {; MyHandler (iems) {. = IEMs; [Object Object]} Object Invoke (Object proxy, Method method, object[] args) [object object]throwable {. GetClass (). getClassLoader ();    [Object Object] [Object Object] System: println ();    [Object Object] [Object Object] Object result = Method.invoke (., args);    [Object Object] [Object Object] System: println (); result;}}
Create a handler, use generics, probably everyone knows, adapt to more classes.
 and then to the proxy class, To be more generic, I inherited a proxy 
with a class.
commonproxy{     <> getproxy (t)     {        class <?>[] clazz = t.getclass (). Getinterfaces ();         classloader classloader = t.getclass (). getClassLoader ();         myhandler handler = myhandler (t);         t =  ()  proxy. (Classloader,clazz,handler);        t;    }}  GetProxy is to create a proxy class for a class. The use of generics does not have to be explained. Here's a look at usage.  iems jdems = new jdems ();  iems jdproxy = commonproxy.getproxy ( Jdems);   jdproxy.deliver ();  jdproxy.call ();  jdproxy.getfood (); 

Of course, the reader can also create an interface and a class that implements the interface itself. The proxy class is then created by the class. Finally we look at the method of Invocationhandler's invoke and what we do with it:

[Object Object] System: println ();    [Object Object] [Object Object] Object result = Method.invoke (., args);    [Object Object] [Object Object] System: println ();

Believe that the use of spring is not unfamiliar, yes, is the pre-notification and post-notification. This is the foundation of Spring AOP. About the proxy class of the dynamic agent is not familiar with, you can refer to this blog: http://www.cnblogs.com/xiaoluo501395377/p/3383130.html

This article is from the "10562970" blog, please be sure to keep this source http://10572970.blog.51cto.com/10562970/1759602

Design mode----Dynamic Proxy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.