Introduction to dynamic agent mechanism

Keywords: Java

Dynamic agent mechanism

I can't understand what dynamic agent is all the time. Maybe I can't understand it completely after writing this article. I hope it can at least reach the level of literacy!

Content is extracted from the book, I hope I can learn more, improve efficiency, improve the realm of it!

In order to implement the dynamic proxy mechanism in Java, we need the support of java.lang.reflect.InvocationHandler interface and java.lang.reflect.Proxy class.

The InvocationHandler interface is defined as follows:

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

Only this invoke() method is defined in this interface. There are three parameters in this method. The meaning of the parameters is as follows:

  • Object proxy: the object being proxied
  • Method method: method to call
  • Object args []: required parameter for method call

The Proxy class is a special operation class to complete the Proxy. It can be used to dynamically generate implementation classes for one or more interfaces. The Proxy class provides the following methods of operation:

    public static Object newProxyInstance (ClassLoader loader, Class<?>[] interface, InvocationHandler h) throws IllegalArgumentException

The implementation class can be generated dynamically through the newProxyInstance() method. The meaning of the parameters in this method is as follows:

  • ClassLoader loader: class loader
  • Class

Get class loader

    package com.javen.reflect;

    /**
     * Created by Javen on 2018/9/12.
     */

    class Person{}
    public class ProxyDemo {
        public static void main(String args[]) {
            Person per = new Person();
            System.out.println(per.getClass().getClassLoader().getClass().getName());
        }
    }

If you want to complete the dynamic proxy, first define a subclass of the InvocationHandler interface to complete the specific operation of the proxy.

    package com.javen.reflect;

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    /**
     * Created by jinweih on 2018/9/15.
     */
    interface Subject {
        String say(String name, int age);
    }

    class RealSubject implements Subject {
        public String say(String name, int age) {
            return "name -->> " + name + "\tage -->> " + age;
        }
    }

    public class MyInvocationHandler implements InvocationHandler{
        private Object object;//True theme
        public Object bind(Object object) {//Binding real operation theme
            this.object = object;
            return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);//Get proxy object
        }

        public Object invoke(Object proxy, Method method, Object[] args)//Dynamic call method
                throws Throwable {
            Object temp = method.invoke(this.object, args);//Call methods to pass in real themes and parameters
            return temp;//Return information of return method

        }

    }

Test dynamic agents

    public class DynaProDemo {
        public static void main (String args[]) {
            MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
            Subject subject = (Subject)myInvocationHandler.bind(new RealSubject());
            System.out.println(subject.say("javen", 23));
        }
    }

Posted by 23style on Tue, 31 Dec 2019 00:49:24 -0800