To summarize AOP mechanism springboot2.x in (comes with demo)

Message: the beginning of a junior school aop it, the teacher is not good, I did not learn, lead and now have a more clear understanding, at that time only know aop,

Aop do not understand the roots of the role, to date, any course that aop difficult to chew, but since usually do not use the interview, hereby summarized.

The following summarizes the start:

Note: I am using springboot2.1 + maven3.6, please set up their own foundation environment

Github project Address: https://github.com/zgq7/devloper-mine/blob/master/src/main/java/com/dev/config/aop/BaseAop.java

1: Import dependent maven

 

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>

 

 

 

2: Create a class as a class section, as follows:

 

package com.dev.config.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

import java.util.Collections;

/**
 * Created by zgq7 on 2019/7/12.
* * 各通知执行顺序: * 1:around-> * 2:before-> * 3:around->retrun code -> * 4:after-> * 5:afterReturning-> * 6:afterThrowing * </p> * <p> * order 执行顺序: * 1: The method of entering the object: order to perform Smaller values * 2: When the object out of the method: order to execute larger values * </ P>
* / @Aspect the @Order ( 1 ) public class BaseAop { Private Final = log LoggerFactory.getLogger Logger ( the this .getClass ()); / ** * write specific cut point * * @apiNote Execution path if it is accurate to class, a line, to the packet if it is accurate. "" get two. "" * @apiNote Execution path if only accurate to the package, can not be used before, after, afterReturuning, around four notification module startup error * * / @Pointcut ( "Execution (public * com.dev. controller.TestController.s ()) " ) public void Aspect () { } / ** * before entering method thereof * * / @Before (value = "Aspect ()" ) public void before (the JoinPoint Joinpoint) { log.info ( "before: Parameter Type: {}" , joinPoint.getArgs ()) ; } / ** * returns the data before the slice (performed before Retrun) * * / @After (value = "Aspect ()" ) public void After (the JoinPoint Joinpoint) { log.info ( "AOP before return: {} " , joinPoint.toLongString ()); } / ** * returns after the slice data * joinPoint.getSignature () method that returns the path back types method ** / @AfterReturning (value = "Aspect ()", returning = "Result" ) public void the afterReturning (Result Object) { log.info ( "AOP After return: returns the result: {}" , Result); } / ** * surround notification * 1: before before * 2: after the afterReturning * * @apiNote 1: parameters of the method must contact the @Around type ProceedingJoinPoint, * @apiNote 2: proceedingJoinPoint.proceed () is the result produced in @AfterReturning the result, the debugger can debug * @apiNote 3: Because @Around to get ahead of the results of the implementation of the purpose of the method, and @Around ahead of @AfterThrowing run, so an exception will be caught in @Around, leading @AfterThrowing not catch exceptions, so @Around mixed with @AfterThrowing * * / //@Around(value = "aspect()") public Object around(ProceedingJoinPoint proceedingJoinPoint) { log.info("aop arrounding :{}", proceedingJoinPoint.getSignature().getName()); try { return proceedingJoinPoint.proceed(); } catch (Throwable throwable) { log.info("aop arrounding error :{}", throwable.getMessage()); //throwable.printStackTrace(); return Collections.EMPTY_MAP; } } /** * 切面报错 **/ @AfterThrowing(value = "aspect()", throwing = "exception") public void afterThrowing(Throwable exception) { log.error("exception occured , msg : {}", exception.getMessage()); if (exception instanceof NullPointerException) log.info("空指针异常"); } }

 

 

The execution order of the plurality of aspect: notice2.1

  1: when entering Objective Method: order to lower the value the execution

  2: When the object out of the method: order to execute larger values

notice2.2: execution order aspect of @ befer, @ after, @ afterReturning, @ around, @ afterThrowing of

. 1 *: around-> 
 * 2: before-> 
 *. 3: around-> Retrun code -> 
 *. 4: after-> 
 *. 5: the afterReturning 
notice2.2.1: value in strict accordance with the order notification may also be a transfer layer go layer

notice2.3: aspect in @around under the method must have a return value, otherwise it is a "problem" of section

* @ApiNote 1: Parameter access method must be at @Around type ProceedingJoinPoint, 
 * @apiNote 2: proceedingJoinPoint.proceed () result in the result @AfterReturning shall be the debug

 

notice2.4: aspect in @around not be mixed with @afterThrowing

 * @ApiNote 3: Because @Around to get ahead of the results of the implementation of the purpose of the method, and @Around ahead of @AfterThrowing run, so an exception will be caught in @Around, leading @AfterThrowing not catch an exception, and therefore @Around @AfterThrowing mix

 

notice2.5: aspect @PointCut in the package or class path execution path problem

* @ApiNote execution path. "" If it is accurate to class, a line, if the bag is to have exactly two. "" 
 * @ApiNote execution path of a packet if only accurate, can not be used before, after, afterReturuning, around four notification module startup error










Badly written summary or bad hope forgive me, please point out a mistake, so fraught;


 

 

Guess you like

Origin www.cnblogs.com/zgq7/p/11310142.html