spring5 dynamic agent AOP

Posted by briglia23 on Tue, 08 Feb 2022 02:26:40 +0100

Agent mode why should we learn agent mode?
Because this is the bottom layer of spring AOP

Our business logic is like this
dao layer - > service layer - > controller layer - > front end

Like static agents, dynamic agents are dynamically generated.
Before you look at dynamic proxies, look at two classes
Proxy: proxy class, which has a very important method newProxyInstance()
InvokationHandler: interface. It has an abstract method, which is a concrete operation to enhance the method

AOP: without changing the source code of the project, the change of a method is AOP aspect oriented programming. The underlying layer of AOP uses dynamic agents

Dynamic proxy operation
First, determine the interface method and interface implementation class to operate

Interface`

public interface Show {

        void gethost();

}

Interface implementation class

public class Host implements Show{
    @Override
    public void gethost() {
        System.out.println("House rental");
    }
}

Dynamic proxy class

package com.mumu.dao01;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class  Proxy1 implements InvocationHandler{

    public static void main(String[] args) {

        /**  Pass the interface to newProxyInstance to use   */
       Class[] interfaces={Show.class};

        /**  Call newProxyInstance method parameter 1 with Proxy class; Loader of the current class, parameter 2; Interface parameter 3 to be cut in; Object of Proxy class */
        Show o = (Show)Proxy.newProxyInstance(Proxy1.class.getClassLoader(), interfaces, new Proxy1(new Host()));
        o.gethost(); //Method of calling interface
    }

    //Pass the implementation class of the interface
    Object obj;

    //Passed in the form of a constructor
    public Proxy1(Object obj){
        this.obj=obj;
    }




    /**  The implementation class method of this interface is the specific operation of the enhanced interface method          */
    @Override   //Parameter 1; Object parameter 2 of interface implementation class; Proxy method parameter 3; array
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("12");

        Object invoke = method.invoke(obj, args);// This call is to call the original method in the interface. Code enhancement methods can be written above or below it
        System.out.println("45");
        return invoke;
    }
}


Learn about dynamic agents
Let's understand AOP operational terminology
Join point: where in the class can be enhanced, these methods are called join points
Entry point: the actual method of real enhancement becomes the entry point
Notification (enhancement): the logical part that is actually enhanced is called notification
Section: action

AOP operation preparation:
1 spring frameworks generally implement AOP operations based on AspectJ
What is AspectJ?
AspevcJ is an independent framework. It is not a part of spring. Generally, AspevcJ is used together for AOP operation

First, understand the pointcut expression:

Means to be right com.mumu.test.Userimp.add()Method for section operation
execution(* com.mumu.test.Userimp.add(..))

It is used in conjunction with annotations
for example

        //Declare which method to cut in @ Before is the pre cut in
    @Before(value = "execution(* com.mumu.test.Userimp.add(..))")


proxy class

@Component
@Aspect//Adding this annotation means that this is a cross-sectional declaration
public class Porxy02 {


        //Declare which method to cut in @ Before is the pre cut in
    @Before(value = "execution(* com.mumu.test.Userimp.add(..))")
    public void  before(){

        System.out.println("Front");

    }

}

configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--        Turn on packet scanning-->
    <context:component-scan base-package="com.mumu.test">   </context:component-scan>



<!--        open aop-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>



Test class

package com.mumu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Userimp userAop = context.getBean("userimp", Userimp.class);
        userAop.add();

    }


}

Note that we need to import packages when using AspectJ

After completing the operation
So the problem is, if there are multiple methods that want to enhance a method
How to determine priority?
Just add annotation @ order (numerical value) on the enhanced class. The smaller the numerical value, the higher the priority

Topics: Spring AOP