Spring proxy mode

The underlying principle of spring AOP is the proxy mode, which can be divided into:

  • Static proxy
  • Dynamic agent

Static proxy

Role analysis:

  • Abstract role: generally, interfaces or abstract classes are used to solve the problem of rent
  • Real role: the role of the agent [landlord]
  • Agent role: acting for real roles. After acting for real roles, we usually do some ancillary operations [intermediary]
  • Client: the person who visits the agent [tenant]

Code steps:

1. Interface

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

2. Real role

//landlord or landlady
public class Host implements Rent{
    public void rent(){
        System.out.println("The landlord wants to rent the house!");
    }
}

3. Agent role

public class Proxy implements Rent {

    private Host host;

    public Proxy(){
    }

    public Proxy(Host host) {
        this.host = host;
    }

    public void rent(){
        seeHouse();
        host.rent();
        contract();
        fare();
    }

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

    public void contract(){
        System.out.println("sign a contract");
    }

    public void fare(){
        System.out.println("Intermediary fee");
    }
}

4. Client access agent role

public class Client {
    public static void main(String[] args) {
        //The landlord wants to rent a house
        Host host = new Host();
        //Agent, the intermediary helps the landlord rent a house, but the agent role usually has some ancillary operations!
        Proxy proxy = new Proxy(host);

        //You don't have to face the landlord, just find an intermediary to rent a house!
        proxy.rent();
    }
}

Benefits of agent mode:

  • It can make the operation of real roles more pure without paying attention to some public businesses
  • The public is handed over to the agent role to realize the division of business
  • When the public business is expanded, it is convenient for centralized management

Disadvantages:

  • A real role will produce a proxy role, double the amount of code and low development efficiency

Dynamic agent

  • Dynamic agents have the same role as static agents
  • The agent class of dynamic agent is generated dynamically, which is not written directly
  • Dynamic agents are divided into two categories: interface based dynamic agents and class based dynamic agents

    • Interface based: JDK dynamic agent [used here]
    • Class based: cglib
    • java bytecode implementation: javasist

Two classes need to be understood: Proxy: Proxy; InvocationHandler: call handler

Dynamic proxy class:

public class ProxyInvocationHandler 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
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log(method.getName());
        //The essence of dynamic agent is to use reflection mechanism
        Object result = method.invoke(target, args);
        return result;
    }

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

Test class:

public class Client {
    public static void main(String[] args) {
        //Real role
        UserServiceImpl userService = new UserServiceImpl();
        //Proxy role, does not exist
        ProxyInvocationHandler pih = new ProxyInvocationHandler();

        pih.setTarget(userService);//Sets the object to proxy
        //Dynamically generate proxy classes
        UserService proxy = (UserService) pih.getProxy();

        proxy.add();
    }
}

Benefits of dynamic agents:

  • It can make the operation of real roles more pure without paying attention to some public businesses
  • The public is handed over to the agent role to realize the division of business
  • When the public business is expanded, it is convenient for centralized management
  • A dynamic agent class represents an interface, which is generally a corresponding type of business
  • A dynamic proxy class can proxy multiple classes as long as it implements the same interface

Keywords: Spring AOP

Added by buluk21 on Thu, 03 Feb 2022 16:12:58 +0200