Review and organize DAY31_ Proxy pattern and spring AOP

What is the agent model?

Proxy mode provides a proxy object for an object, and the proxy object controls the reference to the original object.

Agent mode classification:

Static agent and dynamic agent, and the implementation methods of dynamic agent - JDK dynamic agent and CGLIB dynamic agent.

Static proxy: the bytecode file of the proxy class exists before the program runs.

Dynamic proxy: dynamically create proxy classes when the program runs.

What is a proxy class?

Provides a proxy for an object to control access to the object.

Static proxy:

For example, when we deposit and withdraw, we need a cash detector. After passing it, we can deposit the money into the bank. This cash detector is like an agent. We can't directly contact the inside. We need to go through the middle cash detector to deposit our money.

  Specific implementation:

public interface IBank {
  public void save();
}
public class Bank implements IBank{
  @Override
  public void save() {
    System.out.println("Bank.save()");
  }
}

public class Proxy implements IBank{
  private Bank bank;
  
  public Proxy(Bank bank) {
    this.bank = bank;
  }
  
  @Override
  public void save() {
    // Judge whether it is legal
    System.out.println("Proxy.save.before");
    bank.save();
    System.out.println("Proxy.save.after");
  }
}

public class Money {
  public static void main(String[] args) {
    Bank bank = new Bank();
    IBank proxy = new Proxy(bank);
    proxy.save();
  }
}

  Why should the proxy class (cash detector) and the target object (bank) access the same interface (IBank)?

In order to keep the behavior of the proxy class consistent with the target object when the client (money) accesses, this is a kind of protection for the target object.

Summarize the role of proxy class: implement the same interface as the target object and can be used to replace the target object; Save a reference to a target object and call the corresponding method of the target object when necessary; Control access to target objects.

Summarize the advantages of static agent: it can extend the function of the target object without modifying the target object.

This is like the Regent in ancient times. Sometimes he started in the name of the emperor and did a lot of things, good or bad. In the eyes of the people at the bottom, as long as the Emperor didn't die, these are the will of the emperor.

Disadvantages of static proxy: if there is a target object, there must be a proxy object, which will produce many proxy objects; Once the method is added to the interface, the proxy class and target object have to be maintained, which is cumbersome.

Dynamic agent: This is mainly about JDK dynamic agent

JDK dynamic agent:   Use the reflection mechanism of JDK to create the object of proxy class.

Note: you do not need to write the proxy object yourself, but you still need to implement the interface; Proxy objects are built dynamically in memory using JDK API.

Specific implementation:

public class ProxyFactory {
    /**
     * Generating proxy objects using dynamic proxies
     * @param target Target object (or implement an interface)
     * @return Proxy object
     */
    public static Object getProxyInstance(Object target) {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(), // The class loader used by the target object
                target.getClass().getInterfaces(), // Interface implemented by the target object
                new InvocationHandler() { // Event handler
                    // This invoke method is equivalent to the qianzi() method in the proxy class MiShu
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        // Object proxy proxy object
                        // Method the method called by method
                        // Parameter of method called by Object[] args
                        System.out.println("ProxyFactory.invoke.before");
                        Object returnValue = method.invoke(target, args);
                        System.out.println("ProxyFactory.invoke.after");
                        return returnValue;
                    }
                }
        );
    }
}

public interface IBank {
  public void save();
}
public class Bank implements IBank{
  @Override
  public void save() {
    System.out.println("Bank.save()");
  }
}

public class Money {
  public static void main(String[] args) {
    Bank target = new Bank();
    IBank proxy = (IBank)ProxyFactory.getProxyInstance(target);
    proxy.save();
  }
}

The advantage of JDK dynamic proxy: you don't need to write your own proxy class

Disadvantages of JDK dynamic proxy: it can only enhance interface methods, but can not implement dynamic proxy for interface classes.

Aiming at the shortcomings of JDK dynamic proxy, if you want to implement the dynamic proxy for interface classes, you can use CGLIB dynamic proxy.

In Spring's AOP programming, if the target object added to the container has an implementation interface, use JDK proxy; If the target object does not implement the interface, use CGLIB proxy.

Keywords: Dynamic Proxy

Added by dotMoe on Fri, 10 Sep 2021 22:10:12 +0300