Lao Wang speaks homemade RPC framework. (two. Dynamic agent)

Source: Internet
Author: User
Tags throwable

(#简介)

What is a dynamic agent? Dynamic agent is the implementation phase does not care who the agent is, and in the run phase to specify which proxy object is, dynamic agent in the framework of the use of very
Wide, such as spring's AOP, the core is the use of dynamic proxy mechanism, let's look at how to implement a dynamic proxy mode

(#实现)

First, let's define an interface.

Public interface ICar {    void run (String name);}

Then we'll define an implementation class

public class Car implements ICar {public    void run (String name) {        System.out.println (name+ "car is Running");    }}

Next, let's implement a generic dynamic Proxy tool class

public class objectproxy{Public    <T> T Create (Final T t) {        return (t) proxy.newproxyinstance ( ObjectProxy.class.getClassLoader (),                T.getclass (). Getinterfaces (), new Invocationhandler () {public                    Object Invoke (Object Proxy, Method method, object[] args) throws Throwable {
Before Invoke return Method.invoke (t, args);
After invoke } );} }

In this tool class we use generics to make the method more generic, and if you have a lack of understanding of generics, see this blog: 1190000002646193

Finally, let's take a look at the call:

  public static void Main (String[]args) {          ICar car = new car ();        ObjectProxy carproxy = new ObjectProxy ();        ICar Proxycar = carproxy.create (car);        Proxycar.run ("Benchi");    }

If there is no accident, running the main method will appear: Benchi car is running. Of course, the most powerful point of using dynamic proxies is that you can do some before and after things when you call this method.

This operability is very large, for some features particularly applicable

(#) Expand

In the reflection of a relatively powerful library cglib, in some specific scenarios using fast reflection is necessary, the common JDK agent is a bit more rubbing, is only the agent implementation of the interface, but not the method of proxy itself

This time to realize the function of dynamic agent will need to use to Cglib, OK, here is a chestnut:

First extend the original implementation class: Add a method of its own nrun, let us implement its proxy

public class Car implements ICar {public    void run (String name) {        System.out.println (name+ "car is Running");    } public    void Nrun (String name) {        System.out.println (name+ "I am my own Run");}    }

  

The first implementation of:

public class Fastproxy {public    <T> t create (Final T t) {        enhancer enhancer = new enhancer ();        Enhancer.setsuperclass (T.getclass ());        Enhancer.setcallback (New Methodinterceptor () {Public            object intercept (Object o, Method, object[] objects, Methodproxy methodproxy) throws Throwable {                return Methodproxy.invoke (t,objects);            }        );        Return (T) enhancer.create ();}    }

The second implementation:

public class Fastproxy {public    <T> t create (Final T t) {        enhancer enhancer = new enhancer ();        Enhancer.setsuperclass (T.getclass ());        Enhancer.setcallback (New Methodinterceptor () {Public            object intercept (Object o, Method, object[] objects, Methodproxy methodproxy) throws Throwable {                Fastclass fastclass = Fastclass.create (T.getclass ());                Fastmethod Fastmethod = Fastclass.getmethod (Method.getname (), method.getparametertypes ());                Return Fastmethod.invoke (t,objects);            }        });        Return (T) enhancer.create ();}    }

There is no difference in the invocation of the main method, so that we implement a dynamic proxy for our own methods.

  

  

Lao Wang speaks homemade RPC framework. (two. Dynamic agent)

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.