(3) Spring Study Notes (AOP)

1. What is AOP

Aspect-oriented programming. AOP can be used to isolate various parts of the business logic, thereby reducing the coupling degree between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development at the same time.

Separate the login module from the permission module.

The underlying principle of AOP:

1. Dynamic proxy in two situations

The first case has an interface, using JDK dynamic proxy

  • Create an interface to implement a class proxy object and enhance the method of the class

The second case where there is no interface, use CGLB dynamic proxy

  • Create a proxy object of a subclass and enhance the method of the class

AOP (JDK dynamic proxy)

1. Use the JDK dynamic proxy to create a proxy object using the method in the Proxy class

java.lang.Object

java.lang.reflect.Proxy

(1) Call the newProxyInstance method

The method has three parameters:

The first parameter, the class loader

The second parameter is the class where the enhanced method is located. The interface implemented by this class supports multiple interfaces

The third parameter is to implement the interface InvocationHandler, create a proxy object, and write enhanced methods.

2. JDK dynamic proxy code

(1) Create an interface and define a method

package com.demo;

public interface UserDao {
    public int add(int a, int b);
    public String update(String id);
}

(2) Create an interface implementation class and implement the method 

 

package com.demo;

public class UserDaoImpl implements UserDao{

    @Override
    public int add(int a, int b) {
        System.out.println("add方法执行了。。。");
        return a+b;
    }

    @Override
    public String update(String id) {
        System.out.println("update方法执行了。。。");
        return id;
    }
}

 (3) Create a proxy object using the Proxy class

package com.demo;

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};
//        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 result = dao.add(1,2);
        System.out.println("result"+result);
    }
}

// 创建代理对象代码
class UserDaoProxy implements InvocationHandler{

    // 1 把创建的是谁的代理对象,把谁传递过来
    // 有参数构造传递
    private Object obj;
    public UserDaoProxy(Object obj){
        this.obj = obj;
    }
    // 增强的逻辑
    @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;
    }
}

AOP terms:

1. Connection point

Which methods in the class can be enhanced, these methods are called connection points

2. Entry point

The methods that are actually enhanced are called pointcuts

3. Notification (Enhanced)

(1) The logical part that is actually enhanced is called notification (enhancement)

(2) Notifications include multiple types: pre-notification, post-notification, surround notification, exception notification, and final notification.

4. Section

is action.

(1) Apply the notification to the pointcut process

AOP operation (preparation):

1. Spring framework general opportunity AspectJ realizes AOP operation

(1) What is AspectJ

AspectJ is not a part of Spring, and is an independent AOP framework. Generally, AspectJ and Spring framework are used together for AOP operations.

2. Realize AOP operation based on AspectJ

  • Based on xml configuration file

  • Implementation based on annotations

3. Introduce AspectJ dependencies into the project project

4. Pointcut expression

(1) Function of entry point expression: know which method in which class to enhance

(2) Grammatical structure:

execution([permission modifier][return type][full class path][method name]([parameter list]))

Example 1: Enhance the add method in the com.demo.dao.BookDao class

execution(* com.demo.dao.BookDao.add(..))

Example 2: Enhance all methods in the com.demo.dao.BookDao class

execution(* com.demo.dao.BookDao.*(..))

Example 3: Enhance all methods in the com.demo.dao package class

execution(* com.demo.dao.*.*(..))

Guess you like

Origin blog.csdn.net/weixin_44516623/article/details/127876691