Shred Spring5 framework (five) AOP introduction

basic concepts

AOP, aspect-oriented programming, the use of AOP can isolate each part of the business logic, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development.

Main intention

Separate the logging, performance statistics, security control, transaction processing, exception handling and other codes from the business logic code. By separating these behaviors, we hope that they can be independent of non-directed business logic methods, and then change these The behavior does not affect the code of the business logic.

Core idea: Do not add new functions by modifying the source code.

Actual case sharing:

For example, we now have a user login function, and now we need to add a new function, which is the function of adding login information recording after login. The recorded information includes login name, login time, login ip address, login operating system, etc. content. We can implement it in two ways. One is to modify the source code and add the function of recording user login information in the login function code. The other way is to add the function of adding login information record without changing the source code through AOP.

The implementation of AOP is as follows:

The underlying realization principle of AOP

The bottom layer of AOP is implemented using dynamic proxy:

There are two ways to implement dynamic proxy

1) The object being proxied has an interface

Use JDK dynamic proxy implementation

Scenes:

 

2) The case where the object being proxied has no interface

Use CGLIB dynamic proxy implementation

Scenes:

AOP (JDK dynamic proxy) implementation principle

Write JDK dynamic proxy code:

1) Create interface UserDao and interface implementation class UserDaoImpl

package org.learn.spring5.dao;

public interface UserDao {
    int add();

    int update(Integer id);

}
package org.learn.spring5.dao.impl;

import org.learn.spring5.dao.UserDao;

/**
 * UserDao的实现类
 */
public class UserDaoImpl implements UserDao {


    public int add() {
        System.out.println("UserDaoImpl add ");
        return 1;
    }

    public int update(Integer id) {

        System.out.println("UserDaoImpl  update");
        return id;
    }
}

2) Create the MyInvocationHandler class to implement InvocationHandler, which is used to implement the enhanced method called by the proxy class

Processing of points. The content of enhanced processing is to execute the invoke method in the invoke method when the proxy class calls the method.

package org.learn.spring5.dao.impl;

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

//创建代理对象调用的程序代码
public class MyInvocationHandler implements InvocationHandler {
    private Object object;
    //有参构造传递
    public MyInvocationHandler(Object object){
        this.object = object;
    }

    //增强的逻辑
    //第一个参数:类加载
    //第二个参数:增强方法所在的类,这个类实现的接口,支持多个接口
	//第三个参数:实现这个接口InvocationHandler,创建代理对象,写增强的方法
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("方法之前处理");

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

        System.out.println("方法之后处理");


        return invoke;
    }
}

3) Use the Proxy class to generate an instance of the proxy object and call the corresponding method of the proxy class.

import org.learn.spring5.dao.UserDao;
import org.learn.spring5.dao.impl.MyInvocationHandler;
import org.learn.spring5.dao.impl.UserDaoImpl;

import java.lang.reflect.Proxy;

class TestProxy {

    public static void main(String[] args) {
        //被增强的类,真实的角色
        UserDaoImpl userDaoImpl = new UserDaoImpl();
        Class[] interfaces = {UserDao.class};  //接口
        //动态生成代理对象的实例并返回,传递谁就生产谁的代理对象
        UserDao dao = (UserDao) Proxy.newProxyInstance(TestProxy.class.getClassLoader(),             	userDaoImpl.getClass().getInterfaces(), new MyInvocationHandler(userDaoImpl));
        int res = dao.update(1);
        System.out.println(res);
    }
}

AOP operating terms

 

  • Junction

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

  • Entry point

    The actually enhanced method is called the entry point

  • Notification (enhanced)

    The actual enhancements are called notifications

    There are many types of notifications:

    Advance notice

    Post notification

    Surround notification

    Exception notification

    Final notice

  • section

    Notify the process of applying to the pointcut

  • Pointcut expression

    (1) Pointcut expression function: know which method in which class to enhance

    (2) Grammatical structure:

    excution (<permission modifier><return type><class path><method name>(<parameter list>))

    (3) Example: Enhance the add method of the org.learn.spring5.UserDao class

    excution(* org.learn.spring5.UserDao.add(..))

    Example: Enhance all methods of the org.learn.spring5.UserDao class

    excution(* org.learn.spring5.UserDao.*(..))

 

Guess you like

Origin blog.csdn.net/java_cxrs/article/details/108353167