Core Java: Small demo for JDK dynamic agent using Java.lang.reflect

Source: Internet
Author: User
Tags object object

Proxy mode is very important in Java architecture, including spring's entire large IOC system is based on proxy mode, and AOP is developed on the basis of proxy mode, which increases the timing of triggering action behavior. Therefore, mastering the proxy mode is a very necessary skill.
Here's a simple demo, To explore how the java.lang.reflect implementation of the proxy mode, focusing on the main method, we clearly for the Flyimpl implementation Class Wing (String) assignment is null, but the final output, but found that the Flyimpl object's fly domain into a "A injected Swing value ...", how did all this happen?

First, define an interface class, fly, which has two methods, one instead of addwing, and one is fly.

Package Com.ziwen.vo;public interface flyable {public    void addwing (String wing);    Public String fly ();

Write an implementation class that Littlebird implement Flyable.

Package Com.ziwen.vo;public class Littlebird implements flyable {    private String wing;        Public String getwing () {        return wing;    }    public void setwing (String wing) {        this.wing = wing;    }    @Override public    void addwing (String wing) {        setwing (wing);    }    @Override Public    String Fly () {        return "wing:" +getwing () + "... fly";    }}


As required by the JDK API, there is an implementation class that implements the invocation:

Package Com.ziwen.vo;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;public class Objecthandler implements Invocationhandler {    private Object target;    Public Objecthandler (Object object) {        This.target = object;    }    @Override Public    object Invoke (Object proxy, Method method, object[] args)            throws Throwable {        if ("addwing". Equals (Method.getname ())) {            args[0]= "A injected swing value.";        }        Return Method.invoke (target, args);}    }

Finally, write a factory method to get the proxy class, for simplicity, and write the main method inside.

Package Com.ziwen.vo;import Java.lang.reflect.proxy;public class Flyfactory {public    static flyable Getflayimpl () {        flyable fly = new Littlebird ();        Return (flyable) proxy.newproxyinstance (                fly.getclass (). getClassLoader (), new class[] {flyable.class},                new Objecthandler (fly));    }    public static void Main (string[] args) {        flyable fly = Flyfactory.getflayimpl ();        Fly.addwing (null);        System.out.println (Fly.fly ());}    }

Core Class Description:
The JDK provides a class named Proxy.newproxyinstance (All-in-one). Where is the first parameter for the loader that specifies which class to use? The second parameter is an array of which interfaces are implemented, and the third parameter is an object that describes the invocation and the type of Invocationhandler it is associated with.
Any dynamic proxy object is associated with a Invocationhandler object, and only this object knows what to proxy. The proxy object invokes the Invoke method in Invocationhandler, which is called by the dynamic proxy object, so that we can extend the Invoke method to achieve many of the things we want to do.

Core Java: Small demo for JDK dynamic agent using Java.lang.reflect

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.