(Java) dynamic proxy

Keywords: Java

Article directory

1, Basic concepts

Static proxy Each proxy class can only serve one interface. If you want to complete all the proxy functions through one proxy, you must use the dynamic proxy function.

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

Definition of InvocationHandler interface:

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

This interface defines an invoke() method, which contains three parameters:

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

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

The newProxyInstance() method can dynamically generate implementation classes:

Class loader:

Get class loader:

class Person{}
public class Root{
    public static void main(String[] args) {
        Person stu = new Person();//Instantiate subclass object
        System.out.println("Class loader:" +
                stu.getClass().getClassLoader().getClass().getName());
    }
}

2, Example operation

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

  1. Class defining MyInvocationHandler
class MyInvocationHandler implements InvocationHandler{
    private Object obj;//True theme
    public Object bind(Object obj){//Bind real topic operation topic
        this.obj = obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                obj.getClass().getInterfaces(),this);//Get proxy object
    }
    public Object invoke(Object proxy, Method method,Object[] args) throws Throwable{//Dynamic call method
        Object temp = method.invoke(this.obj,args);//Call methods, passing in real themes and parameters
        return temp;
    }
}

Here, the bind() method of MyInvocationHandler class accepts the real subject implementation of the proxy, and then overwrites the invoke() method in the InvocationHandler interface

  1. Defining interfaces
interface Subject{//Define the Subject interface
    public String say(String name,int age);//Define abstract method say
}
  1. Define the real theme implementation class
//Define the real theme implementation class
class RealSubject implements Subject{//Real implementation class
    public String say(String name,int age){//Override the say() method
        return "Full name:" + name + ",Age:" + age;//Return information
    }
}

The interface and the real topic class are defined here, so that the object of the real topic class can be directly passed into the bind() method of MyInvocationHandler class during operation.

  1. Test dynamic agents
public class Test{
    public static void main(String[] args) {
        MyInvocationHandler handler = new MyInvocationHandler();//Instantiate agent action class
        Subject sub = (Subject)handler.bind(new RealSubject());//Bound object
        String info = sub.say("Java",30);//Calling methods through dynamic agents
        System.out.println(info);
    }
}
593 original articles published, 269 praised, 240000 visitors+
His message board follow

Posted by deltawing on Sat, 22 Feb 2020 02:01:05 -0800