Using proxy technology in the JDK to implement dynamic proxies

Source: Internet
Author: User

Let me give you an example of how the agent works:


Examples of real life: I have a college holiday, I want to relax to the ocean Pavilion to buy a ticket, I saw the ocean pavilion sitting in front of a person, it seems to sell tickets, so I went to him to buy tickets, so he went out and said to me to get the tickets, in the process of his ticket to find another person, the man gave him the ticket, He gave me the ticket, but in the process I did not know anything, I know I want to take the ticket, he gave me the ticket is OK.


Java Example: The client uses an interface to refer to an object, and then use this = interface to invoke some methods, on the surface I only see the reference to this interface, and the program in the run time to find the specific object, this is polymorphic, so I can completely swap this object, so that in the inside do some hands and feet, such as logging, performance statistics, security control, transaction processing, exception handling and so on. Below, this example uses the proxy technique to do a bit of ArrayList to this class.


public class Arraylistproxy {Private Object Target;public object Gettarget () {return target;} public object Createproxy (object target) {This.target = Target;return proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), new Invocationhandler () {@Overridepublic object Invoke (object Proxy, method, object[] args) throws Throwable {System.out.println ("sample, I am a virus, I will stop you today, love how to"); Object result = Method.invoke (Gettarget (), args); return result;});}}

Here is the test class:


public class Proxytest {@Testpublic void Testproxy () {Collection con = (Collection) new Arraylistproxy (). Createproxy (New A Rraylist ()); addelement (con);} public void addelement (Collection con) {Boolean flag = Con.add ("I'm going to add elements to this collection, no one can stop, hahaha!!!") "); SYSTEM.OUT.PRINTLN (flag);}}

We wanted to add elements to the collection, and we could add some of the code we wanted before adding it to our goal. The basic principles of AOP technology in spring are also implemented in this way, the delegate class must have an interface to be able, if there is no interface is not feasible, but we can use Cglib, he will inherit this delegate class, the basic principle is not much difference.

Using proxy technology in the JDK to implement dynamic proxies

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.