Proxy mode--dynamic proxy--jdk proxy

dynamic proxy

jdk proxy--based on interface proxy

cglib--class-based proxy

javassist – based on bytecode

A jdk dynamic proxy class represents an interface and generally belongs to a business. Without changing the source code, it is easy to carry out additional processing modifications at low cost.

The jdk proxy is mainly implemented through the Proxy class and InvacationHandler interface in the java.lang.reflect package. First, take a look at the source code of these two classes.

InvacationHandler interface

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

There is only one invoke method. This method handles the proxy instance and returns the result. It needs to be called using the reflection method, method.invoke(Object obj, Object[] args). The first parameter is the interface class to be proxied. The second one is the parameters related to the corresponding method.

Back to the source code, the first parameter is theoretically the object to be proxied, but it is not actually used. The second parameter is the corresponding method in the interface, and the third parameter is the corresponding parameter.

Secondly, look at the Proxy class

It contains an important and commonly used method

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

Get an instance of the corresponding proxy class. This method is usually used to obtain the proxy instance dynamically.

Steps to use jdk proxy

1. Create a class that implements the interface InvocationHandler, which must implement the invoke method
2. Create the proxied class and interface
3. Create a proxy through Proxy’s static method
newProxyInstance(ClassLoaderloader, Class[] interfaces, InvocationHandler h)
4. Through the proxy Calling method
implementation code:

Real role (object to be proxied):

package com.heaboy.proxyExcise.JDkProxy;

/**
 * @Author:XK
 * @Date: Created in 11:23 2022/2/19
 * @Description:
 **/
public class Host implements Rent {

    @Override
    public void rent() {
        System.out.println("房东出租房子");
    }

    @Override
    public void run() {
        System.out.println("房东跑了");
    }
}

Abstract role (interface to be proxied)

package com.heaboy.proxyExcise.JDkProxy;

/**
 * @Author:XK
 * @Date: Created in 11:20 2022/2/19
 * @Description:
 **/
public interface Rent {
    public void rent();
    public void run();
}

Inherit the role of InvacationHanler and dynamically generate proxy objects

package com.heaboy.proxyExcise.JDkProxy;

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

/**
 * @Author:XK
 * @Date: Created in 15:45 2022/2/19
 * @Description:
 **/
public class RentProxy implements InvocationHandler {

    private Rent target;

    public  RentProxy(Rent target) {
        this.target = target;
    }

    //动态生成代理对象
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
    }

    //处理实例的方法,并且加以附属操作
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        seeHouse();
        Object result=method.invoke(target,args);
        fare();
        return result;
    }
    public void seeHouse(){
        System.out.println("中介带你去看房");
    }
    public void fare(){
        System.out.println("中介带你收费");
    }
}

Customer role; calling agent

package com.heaboy.proxyExcise.JDkProxy;
/**
 * @Author:XK
 * @Date: Created in 15:52 2022/2/19
 * @Description:
 **/
public class client {
    public static void main(String[] args) {
        Host host = new Host();

        RentProxy rentProxy = new RentProxy(host);

        Rent proxy = (Rent)rentProxy.getProxy();

        //proxy.rent();

        proxy.run();
    }
}

Guess you like

Origin blog.csdn.net/Yoke______/article/details/123019984