Learn spring 5 architecture from scratch -- the underlying mechanism of AOP: proxy mode

Keywords: Java Scala Spring AOP

Proxy mode:

  • Static proxy
  • Dynamic agent

Before learning AOP, we need to understand the agent pattern

Static proxy

Static agent role analysis

  • Abstract role: generally implemented using interfaces or abstract classes

  • Real role: the role represented

  • Agent role: agent real role; After representing a real role, you usually do some ancillary operations

  • Customer: use the agent role to perform some operations

code implementation

Rent. Java is an abstract role

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

Host. Java is the real role

//Real role: landlord, the landlord wants to rent the house
public class Host implements Rent{
    public void rent() {
        System.out.println("House rental");
    }
}

Proxy. Java is the proxy role

//Agent role: Intermediary
public class Proxy implements Rent {
    private Host host;
    public Proxy() { }
    public Proxy(Host host) {
        this.host = host;
    }
    //Rent a house
    public void rent(){
        seeHouse();
        host.rent();
        fare();
    }
    //House viewing
    public void seeHouse(){
        System.out.println("Show the tenant");
    }
    //Intermediary fee
    public void fare(){
        System.out.println("Intermediary fee");
    }
}

Client. Java is the client

//Customers, general customers will find agents!
public class Client {
    public static void main(String[] args) {
        //The landlord wants to rent a house
        Host host = new Host();
        //The intermediary helps the landlord
        Proxy proxy = new Proxy(host);
        //You go to the agency!
        proxy.rent();
    }
}

analysis:
In this process, you directly contact the intermediary, just like in real life, you can't see the landlord, but you still rent the landlord's house through the agent, which is the so-called agent model

benefit

  • Can make our real role more pure. No longer pay attention to some public things
  • The public business is completed by the agent, which realizes the division of business
  • When the public business expands, it becomes more centralized and convenient

shortcoming

With more classes and more proxy classes, the workload becomes larger and the development efficiency decreases

Dynamic agent

  • The role of dynamic agent is the same as that of static agent
  • The proxy class of dynamic proxy is dynamically generated. The proxy class of static proxy is written in advance
  • Dynamic agents are divided into two categories: one is interface based dynamic agents, and the other is class based dynamic agents
    • Dynamic agent based on interface -- JDK dynamic agent
    • Class based dynamic proxy cglib
    • javasist is now used to generate dynamic proxies
    • We use the native code of JDK here, and the rest are the same

The dynamic proxy of JDK needs to know two classes

Core: InvocationHandler and Proxy. Open the JDK help document and have a look

[InvocationHandler: call handler]

Object invoke(Object proxy, method method, Object[] args);
//parameter 
//Proxy - the proxy instance that calls the method 
//Method - the method corresponds to the instance that invokes the interface method on the proxy instance. The declared class of the method object will be the interface declared by the method, which can be the super interface of the proxy interface of the proxy class inheriting the method. 
//args - array of objects containing method calls that pass the parameter values of the proxy instance, or null if the interface method has no parameters. The parameters of the primitive type are contained in an instance of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

[Proxy: Proxy]

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

code implementation

Rent. Java is an abstract role

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

Host. Java is the real role

//Real role: landlord, the landlord wants to rent the house
public class Host implements Rent{
    public void rent() {
        System.out.println("House rental");
    }
}

ProxyInvocationHandler. java is the proxy role

public class ProxyInvocationHandler implements InvocationHandler {
    private Rent rent;
    public void setRent(Rent rent) {
        this.rent = rent;
    }
    //Generate an agent class, focusing on the second parameter to obtain the abstract role to be represented! It used to be a role, but now it can represent a kind of role
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                rent.getClass().getInterfaces(),this);
    }
    // Proxy: proxy class method: the method object of the calling handler of the proxy class
    // Process method calls on proxy instances and return results
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        seeHouse();
        //Core: essence is realized by reflection!
        Object result = method.invoke(rent, args);
        fare();
        return result;
    }
    //House viewing
    public void seeHouse(){
        System.out.println("Show the tenant");
    }
    //Intermediary fee
    public void fare(){
        System.out.println("Intermediary fee");
    }
}

Client . java

//tenant
public class Client {
    public static void main(String[] args) {
        //Real role
        Host host = new Host();
        //Call handler for proxy instance
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        pih.setRent(host); //Put the real character in!
        Rent proxy = (Rent)pih.getProxy(); //Dynamically generate the corresponding proxy class!
        proxy.rent();
    }
}

Core:
A dynamic agent generally represents a certain type of business. A dynamic agent can represent multiple classes, and the agent is the interface

Benefits of dynamic agents

  • It can make our real role more pure and no longer pay attention to some public things
  • The public business is completed by the agent, which realizes the division of business
  • When the public business expands, it becomes more centralized and convenient
  • A dynamic agent, generally acting for a certain kind of business
  • A dynamic proxy can proxy multiple classes, and the proxy is the interface

Posted by Tyree on Fri, 01 Oct 2021 16:06:30 -0700