spring dynamic proxy mode

Posted by sp@rky13 on Thu, 20 Jan 2022 01:40:30 +0100

There are two main ways to implement dynamic agent:

  • Interface based dynamic agent: JDK dynamic agent
  • Class based dynamic proxy: cglib

The following shows the JDK dynamic proxy

1. An interface and a class that JDK dynamic agent needs to understand

1.1 InvocationHandler

InvocationHandler is the interface that needs to be implemented by the class generating the proxy instance, and then the invoke() method in the interface needs to be implemented to process the proxy instance in this method

1.2 Proxy

Proxy is the parent class of all proxy instances. It provides static methods to create dynamic proxy instances

2 code demonstration I

2.1 create an abstract object

//Rent a house
public interface Rend {
    public void rend();
}

2.2 create a real object

public class Homeowner implements Rend {
    @Override
    public void rend(){
        System.out.println("The landlord rented the house");
    }
}

3.3 create a class that generates proxy instances (core)

//This class is used to generate proxy instances
public class ProxyInvocationHandle implements InvocationHandler {

    //Proxy interface
    private Rend rend;

    public void setRend(Rend rend) {
        this.rend = rend;
    }

    //Generated proxy class
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),rend.getClass().getInterfaces(),this);
    }

    //Process the proxy instance and return the result
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //The essence of dynamic agent is to use reflection mechanism!
        seeHouse();
        Object result = method.invoke(rend, args);
        fare();
        return result;
    }

    public void seeHouse(){
        System.out.println("The agent will show you the house");
    }
    public void fare(){
        System.out.println("Intermediary fee");
    }
}

3.4 create customer class

public class Client {
    public static void main(String[] args) {
        //Real role
        Homeowner homeowner = new Homeowner();
        //Agent role: not now
        ProxyInvocationHandle pih = new ProxyInvocationHandle();
        //Handle the interface object we want to call by calling the program processing role!
        pih.setRend(homeowner);
        Rend proxy = (Rend) pih.getProxy();//The proxy here is generated dynamically. We didn't write it
        proxy.rend();
    }
}

3.5 implementation results

3 code demonstration II

3.1 create an abstract object

public interface UserService {
    public void query();
    public void add();
    public void delete();
    public void update();
}

3.2 create a real object

public class UserServiceImpl implements UserService{
    @Override
    public void query() {
        System.out.println("Query user");
    }
    @Override
    public void add() {
        System.out.println("Add user");
    }
    @Override
    public void delete() {
        System.out.println("delete user");
    }
    @Override
    public void update() {
        System.out.println("Modify user");
    }
}

3.3 create a class that generates proxy instances (core)

//Use this class to automatically generate proxy classes
public class ProxyInvocationHandle implements InvocationHandler {

    //Proxy interface
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //Generated proxy class
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    //Process the proxy instance and return the result
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //The essence of dynamic agent is to use reflection mechanism!
        log(method.getName());
        Object result = method.invoke(target, args);
        return result;
    }

    public void log(String msg){
        System.out.println("Yes"+msg+"method");
    }
}

3.4 create customer class

public class Client {
    public static void main(String[] args) {
        //Real role
        UserServiceImpl userService = new UserServiceImpl();
        //Proxy role, does not exist
        ProxyInvocationHandle pih = new ProxyInvocationHandle();
        pih.setTarget(userService);//Sets the object to proxy
        //Dynamically generate proxy classes
        UserService proxy = (UserService) pih.getProxy();
        proxy.add();
    }
}

3.5 implementation results

4 Summary

  • Dynamic agent solves the problem that static agent creates too many agent classes, which leads to the reduction of development efficiency
  • The role of dynamic agent is the same as that of static agent
  • The proxy class of dynamic proxy is generated dynamically The proxy class of static proxy is written in advance
  • A dynamic agent, generally acting for a certain kind of business
  • A dynamic proxy can proxy multiple classes, and the proxy is the interface
  • It can make our real role more pure Stop paying attention to some public things
  • Public business is done by agents The division of business is realized
  • When the public business expands, it becomes more centralized and convenient