java dynamic proxy (pattern) InvocationHandler (add content before or after method execution in the class)

Dynamic proxy is a kind of Java reflection.

When we get an object and want to dynamically append some operations before and after each method is called, we will use java dynamic proxy.

The code below:

First define an interface:

package com.liuyx;

public interface Itf {
    public abstract void printMe();

    public abstract void printSth(String me);
}

Next is its implementation:

package com.liuyx;

public class Cls implements Itf {

    @Override
    public void printMe() {
        System.out.println("I'm Cls!");
    }

    @Override
    public void printSth(String str) {
        System.out.println(str);
    }

}

Our purpose is to add some printing operations before and after the execution of the two methods of the object of the Cls class through dynamic proxy technology.

Now we implement an InvocationHandler and write all the operations we want to append to the delegated through the proxy in the invoke method:

package com.liuyx;

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

public class StandardInvocation implements InvocationHandler {

    private Object obj;
    
    StandardInvocation(Object obj){
        this.obj=obj;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before method excute!");
        Object result = method.invoke(obj, args);
        System.out.println("after method excute!");
        return result;
    }

}
First of all, there is an obj in it. This obj is necessary. Since we want to be a proxy, we must know who we are proxying for. The obj here is the proxy.
In the final analysis, a dynamic proxy is also a proxy, and a proxy is required in the proxy mode.
Then there are the three parameters of invoke, the first parameter is the agent, if you want to do some operations on the agent, you can use this parameter; the second is the method to be executed, and the third is the parameters required to execute the method .

When you execute a method of the agent, the invoke method is the last thing to run.

pxy is the agent of c.

Finally, a slightly improved way of writing:

package com.liuyx;

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

public class InvocationHandlerTest implements InvocationHandler {

    private Object target;

    Object bind(Object i) {
        target = i;
        Object warpedItf;
        warpedItf = Proxy.newProxyInstance(target.getClass().getClassLoader(), i.getClass().getInterfaces(), this);
        return warpedItf;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before method excute!");
        method.invoke(target, args);
        System.out.println("after method excute!");
        return null;
    }
    
    public static void main(String[] args) {
        Cls c = new Cls();
        InvocationHandlerTest pxy = new InvocationHandlerTest();
        Itf itf = (Itf)pxy.bind(c);
        itf.printSth("Hello");
    }

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733438&siteId=291194637