Java springboot proxy and AOP aspect oriented programming basics

Agency mode [Commission mode]

The agent is understood as the agent of the star
Fans visit the proxy object. After the proxy object processes the fans' request, it sends the request to the stars

Static proxy [target object of static proxy operation]

The following nouns are all made up by me. I can understand them. awa

Target class

The target class is easy to understand. It is the class that the main function ultimately needs to access

Main function class

The main function class is also well understood, that is, what method of the target class the main function will call

proxy class

Proxy class: take the target class as its own attribute, and then create a method with the same name as the method of the target class. I call it a proxy method. In the proxy method, the request of the main function is processed, and the method with the same name of the target class is called

For proxy classes, all methods of the target class must have corresponding mappings (mapping means the method name is the same)
The proxy class also wraps these methods in comparison to those of the target class

// proxy class
class StaticProxy
{
    // This is an instance object of the target class
	private Subject subject; 
    // Constructor
    public StaticProxy(Subject subject)
    {
        this.subject = subject;
    }
    // request is a method of the target class
    public void request()
    {
        // Operation omission before the target class executes the request function
        subject.request();
        // Operation omission after the target class executes the request function
    }
}

// Main function class
// The main function class will declare an object instance of the target class Subject
// Then use this subject as a parameter to instantiate a StaticProxy object
// Then call the request function through StaticProxy

Dynamic proxy [intercept the method execution of the target object]

The key point is the proxy processor. Through the InvocationHandler interface, the proxy processor automatically generates proxy objects with the same type as the target class, and accesses the proxy object [equivalent to accessing the target class], which will be intercepted and processed by the invoke method of the proxy processor

  • Implement InvocationHandler interface
    • Implement the invoke method. The formal parameter method of the invoke method refers to the call of the proxy object method. Invoke internally responds to the request according to the method
// The target object implements an interface Subject
// Target object:
class SubjectImpl implements Subject
{
   	// Several methods
}
// Agent processor
class ProxyHandler implements InvocationHandler
{
    private Subject subject; // Declare interface objects
    // Constructor, same as static proxy constructor, omitted
    @Override
    public Object invoke(Object proxy,Method method,Object[] args) throws Throwable
    {
        // Operation omission before execution
        // Call the real target object
        Object result = method.invoke(subject.args);
        // Operation omission after execution
    }
}
// Main function class
// In the main function, the Proxy Object of the target class is generated through the newProxyInstance method of the Proxy class [the specific parameters are Baidu, which is not covered in the tutorial]. The return value is the Object type. In order to be consistent with the target class, the type needs to be cast
// The proxy class is used to call the method of the target class, and the method execution will be intercepted by the invoke method

How to handle multiple interfaces with the same name

The proxy processor can proxy many interfaces at the same time, so a situation may occur: some methods of these interfaces have the same name. For interfaces with the same name [name and formal parameter], the processing method is

It is called by the method of the first interface by default [that is, it is related to the position of the method in the code]

AOP aspect oriented programming

One understanding is: specify the method operation mode in the configuration file [for example, method A is executed before method B is executed, so that different people can write methods A and B at the same time, and finally write the configuration file to improve the programming efficiency]

Another understanding is: take out the methods that we all need to use as a separate facet class. When we need to use this method in the future, we can directly call the facet class

sure:
Decoupling code
When the business logic changes, you can only modify the aspect classes
Improve code utilization!!!

Dynamic agent implementation AOP [implementation of compatibility configuration file]

That is, in the invoke method, judge the state of the method, and determine the execution of the method according to its before | after state

before | after status is viewed in the configuration file [xml], which specifies the execution sequence of methods, with the format as follows

// To read the xml configuration file, you need to import the file path [relative path]
XmlReader.readXml("**/aops.xml") 
// In order to be able to listen for changes in the configuration file [absolute path [folder where the configuration file is located]]
ResourceListener.addListener("**/**")
# B must execute A before execution. The same is true for after
<aop>
    <method> method A </method>
    <type> before </type>
    <method> method B </method>
</aop>

[specific code will be supplemented later] [dig a pit here in advance]

Tags: Java Spring Boot Proxy Pattern Software Engineering

Posted by Zetusko on Sat, 04 Jun 2022 03:41:00 +0530