Spring AOP explanation of related terms and simple to use

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/TreeShu321/article/details/102532208

What is Aspect Oriented Programming

Baidu Encyclopedia is defined as: In the software industry, AOP is the abbreviation for Aspect Oriented Programming, meaning: Aspect Oriented Programming, dynamic agent program features a technology to achieve unity maintained by way of pre-compiled and run. AOP is a continuation of OOP, is a hot spot in software development, an important element is the Spring framework, is a functional programming Yansheng Fan type. AOP can use to isolate each part of the business logic such that the business logic to reduce the degree of coupling between the parts, improve the reusability of the program, while improving efficiency of development.

AOP related terms

Notification (Advice)

In terms AOP, the operating section is referred to as a notification. Notification section defines what and when to use, notice also addresses when.
spring section there are five types of notifications

  • Pre-notification (Before): call notification function before the target method is called;
  • After returning advice (After): notification after the target method call is completed, then what is not concerned about the method of input;
  • Return notification (After-returning): call notification after successful execution of the target method
  • Exception notification (After-throwing): call notification when the target method throws an exception
  • Around advice (Around): After notification includes a method to be notified, before the method is notified of calls and calls to perform custom behavior

Connection point (Join point)

A specific location program execution: As classes begin initializing, after initialization class, the class before a method call, after call, after the method throws an exception. Class or a specific section of the program code has a boundary point having properties, in particular these points called "point of attachment." Spring supports only the point of attachment of the method, i.e., only before the method call, method call, the program execution method throws these points before and after weaving reinforcing and method call exception. The connection point is determined by the two messages: a first program execution point is represented by the process; the second point is represented by a relative orientation.

Tangent point (Pointcut)

Each program type has a plurality of connection points, such as a class has two methods, these methods are two connection points, i.e. the point of attachment is the object classes present in the objective. AOP by "tangent point" target a particular connection point. The point of attachment is equivalent to the records in the database, the point of tangency corresponding to the query condition. Relationship not one tangent point and the connection point, a cut point may match a plurality of connection points. In Spring, org.springframework.aop.Pointcut tangent point described by the interface, it is used as a query-based methods and the point of attachment, the AOP Spring rule engine is responsible for parsing the tangent point query set, find the corresponding connector point. In fact, precisely, can not be called a query connection point, because the connection point is before the method execution, such as after the execution, including the location information of the specific program execution point, and cut to a point positioning method only, so if you want to target specific the connection point, also need to provide location information.

Weaving (Weaving)

It is added to enhance the weaving process based on the specific target point of attachment. AOP as a loom, the target class, or introduce enhanced seamlessly woven together by AOP this loom. Depending on the implementation techniques, the AOP weaving three ways:
A, compile weaving, which requires special Java compiler.
B, weaving of class loading, which requires the use of a special class loader.
C, dynamic proxy weaving, adding a reinforcing manner as subclassing target class at runtime.
Spring dynamic proxy weaving, while the weaving AspectJ using compile and class loading of weaving.

Section (Aspect)

And the notification section is the binding point of tangency

springboot simple example of integration aop

Code on github

package com.sl.aop;

import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

/**
 * @author shuliangzhao
 * @Title: AopLog
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/10/13 15:18
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class AopLog {

    @Pointcut("execution(* com.sl..*.*(..))")
    public void pointcut() {

    }
    
    @Before("pointcut()")
    public void before() {
        System.out.println("方法执行前...");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("方法执行后...");
    }

    @Around("pointcut()")
    public void around() {
        System.out.println("环绕执行方法...");
    }

    @AfterThrowing("pointcut()")
    public void ffterThrowing() {
        System.out.println("方法执行失败...");
    }
}

Glossary:
Example: execution (* com.sl.service ... * * (...).)

The entire expression can be divided into five parts:

1, execution () :: expression body.

2, a first asterisk: indicates the return type, * represents the number of all types.

3, package name: indicates the need to intercept the package name, the latter two periods represents the current sub-packet and a current packet of all packets, com.sample.service package, all descendants of class methods under the package.

4, the second asterisk: Indicates the class name, asterisk represents all classes.

5 * (...): This last asterisk indicates the method name, asterisk represents all the way back inside the parentheses represents the parameters of the method, two periods indicate any parameter

Guess you like

Origin blog.csdn.net/TreeShu321/article/details/102532208