The underlying principle of AOP-Spring framework

What is AOP

1. What is AOP
(1) Aspect-oriented programming (aspect), using AOP can isolate various parts of business logic, thereby reducing the coupling between various parts of business logic, improving the reusability of the program, and improving the Development efficiency.
(2) Popular description: Add new functions to the main function without modifying the source code
(3) Use login examples to illustrate AOP
Insert picture description here

The underlying principle of AOP

There are two cases of dynamic proxy

  • Use JDK dynamic proxy when there are interfaces
  • If there is no interface, use CGLIB

(1) Use JDK dynamic proxy if there are interfaces

Create interface implementation class proxy object, enhance class method
Insert picture description here

(2) If there is no interface, use CGLIB dynamic proxy

Create proxy objects of subclasses and enhance class methods
Insert picture description here

Realization of JDK dynamic proxy

Insert picture description here

方法有三个参数:
第一参数,类加载器 
第二参数,增强方法所在的类,这个类实现的接口,支持多个接口 
第三参数,实现这个接口 InvocationHandler,创建代理对象,写增强的部分

step

(1) Create interface and define method
Insert picture description here
Insert picture description here

(2) Create an interface implementation class and implement methods
Insert picture description here
(3) Use the Proxy class to create an interface proxy object

  • The parameterized construction method passes the enhanced object
  • invoke object enhancement
    Insert picture description here
  • Code
package com.LinXiaoDe.spring5;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JDKProxy {
    
    
    public static void main(String[] args) {
    
    
        Class[] interfaces={
    
    UserDao.class};
        //1.类加载器
        //2.增强方法所在的类,这个类实现的接口,支持多个接口
        //3.实现这个接口 InvocationHandler,创建代理对象,写增强的部分
        /*匿名内部类,可以写在内部
        Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return null;
            }
        });*/
        UserDaoImpl userDao=new UserDaoImpl();
        UserDao dao= (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces,new UserDaoProxy(userDao));
        int res=dao.add(1,2);
        System.out.println("result: "+res);
    }
}
//创建代理对象,实现InvocationHandler,用于增强
class UserDaoProxy implements InvocationHandler {
    
    

    //1.把创建是谁的代理对象,那么把谁传进来
    //有参构造方式传入
    private Object obj;
    public UserDaoProxy(Object obj)
    {
    
    
        this.obj=obj;
    }
    //2.只要对象被创建,那么方法将会被调用
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    
        //方法之前
        System.out.println("方法执行之前,"+method.getName()+" ,   传递的参数:"+ Arrays.toString(args));

        //被增强方法执行
        Object res=method.invoke(obj,args);

        //方法之后
        System.out.println("方法之后执行:"+obj);
        return res;
    }
}

Guess you like

Origin blog.csdn.net/weixin_44307065/article/details/107320443