Dynamic proxy for Spring learning 10

Posted by DarkWater on Sun, 12 Dec 2021 00:12:57 +0100

preface

advantage

  • It can make the operation of real characters more pure! Don't pay attention to some public business
  • The public business is handed over to the agent, which realizes the division of business
  • When the public business is expanded, it is convenient for centralized management

Disadvantages:

  • A real role will produce an agent, double the amount of code and reduce the development efficiency

1, Think about how to deal with this shortcoming and reduce the amount of code?

In the last section, we summarized the advantages and disadvantages of static agents. We know that a role will produce an agent class. In reality, a database cannot be monopolized by one person. Many people need to access the database at the same time, so many people need more agents and double the amount of code.
In the past, we directly created a proxy class and instantiated an object. If there is a method to automatically generate an object of a proxy class for you, that is, is it possible to encapsulate the whole set of proxies into one method???
This method is really used for processing. InvocationHandler creates a proxy handler to implement its method, which is used to generate proxy classes, process proxy instances, and return results.

2, Dynamic agent

Code steps:
1. Interface
2. Real role
3.
4. Client access agent role
The following is an example of (pseudo) database addition, deletion, modification and query:

1. Required classes

Service interface

package com.shan.demo04;

public interface UserService {

    void add();

    void delete();

    void update();

    void query();

}

Business implementation class

package com.shan.demo02;
//Changing the business code is a big taboo in the company, because you can crash the whole project by changing the code
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("Added a user");
    }

    @Override
    public void delete() {
        System.out.println("A user was deleted");
    }

    @Override
    public void update() {
        System.out.println("Modified a user");
    }

    @Override
    public void query() {
        System.out.println("A user was queried");
    }
}

Customer class

package com.shan.demo04;

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

Agent handler

package com.shan.demo04;

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

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());
        Object result = method.invoke(target, args);
        return result;
    }

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

}

2. Analysis

The customer wants to modify the database, but he can't, so the customer finds the handler, and the handler automatically generates an agent class to help him modify the database. He can also do his own business. In order to know more clearly which method of the database is used, he adds a log output without changing the original business code, It will not affect the business.

As long as the end customer calls the method of the agent handler, he can modify the database without directly facing the database! And the agent class can have its own things (plus a log output), which will not affect the business!
This is the role of the agent!

summary

Benefits of dynamic agents:

  • It can make the operation of real characters more pure! Don't pay attention to some public business
  • The public business is handed over to the agent role! 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!

The author has something to say

It's not easy to create a blog. I hope to see the readers here move your little hand and point out a praise. If you like a little partner, you can click three times. The author is here to thank you.

Topics: Java Maven Spring intellij-idea