1. Add maven dependency annotations
<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>Copy the code
2. Add AOP classes
@Component
@Aspect
public class JournalServiceAspect {
}Copy the code
3. Set the cutting point
/** private final String POINT_CUT ="execution(* com.xx.xx.. * (..) )";
@Pointcut(POINT_CUT)
private void pointcut() {}Copy the code
4. Configure pre-notification
/** ** @param joinPoint */ @before (value = POINT_CUT) public void Before(joinPoint joinPoint){logger.info("Advance notice"); Object[] obj = joinPoint.getargs (); // Information about the AOP proxy class joinPoint.getThis(); // The target object of the proxy joinPoint.gettarget (); // Signature Signature = joinPoint.getSignature(); // Which method logger.info("Which method is the proxy?"+signature.getName()); // The AOP proxy class name logger.info("Name of the AOP proxy class"+signature.getDeclaringTypeName()); / / AOP proxy class class (class) information signature. GetDeclaringType (); / / get RequestAttributes RequestAttributes RequestAttributes = RequestContextHolder. GetRequestAttributes (); HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); // If you want to obtain Session information, you can write: //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION); / / get request parameter Enumeration < String > Enumeration = request. GetParameterNames (); Map<String,String> parameterMap = Maps.newHashMap();while (enumeration.hasMoreElements()){
String parameter = enumeration.nextElement();
parameterMap.put(parameter,request.getParameter(parameter));
}
String str = JSON.toJSONString(parameterMap);
if(obj.length > 0) {
logger.info("Requested parameter information is:"+str); }}Copy the code
Note: JoinPoint and RequestContextHolder are used. 1) Through JoinPoint, you can obtain the signature information of the notification, such as the target method name and target method parameter information. 2) Use RequestContextHolder to obtain request information and Session information. 5. Configure post-return notification
/** * return notification * note here: * If the first parameter in the returning argument is JoinPoint, the second parameter is the returned value * returning: Specifies that a post-return notification can be executed only if the target method returns the value and the corresponding parameter type of the notification method. Otherwise, no post-return notification can be executed. * Notification method parameters of type Object will match any target return value * @param joinPoint * @Param keys */ @AfterRETURNING (value = POINT_CUT,returning ="keys")
public void doAfterReturningAdvice1(JoinPoint joinPoint,Object keys){
logger.info("Return value of the first post-return notification:"+keys);
}
@AfterReturning(value = POINT_CUT,returning = "keys",argNames = "keys")
public void doAfterReturningAdvice2(String keys){
logger.info("The return value of the second post-return notification:"+keys);
}Copy the code
6. Post exception notification
/** * Post-exception notification * defines a name that matches one of the parameters of the notification implementation method. When the target method returns with an exception thrown by it, it passes the exception thrown by the target method to the notification method. * Throwing: restricts post-exception notification to only the exception thrown by the target method and the exception type of the notification method parameter. Otherwise, no exception will be executed. * For Throwing the notification method parameter Throwable will match any exception. * @param joinPoint * @param exception */ @AfterThrowing(value = POINT_CUT,throwing ="exception")
public void doAfterThrowingAdvice(JoinPoint JoinPoint,Throwable Exception){// Target method name: Logger.info (joinPoint.getSignature().getName());if(exception instanceof NullPointerException){
logger.info("A null pointer exception has occurred!!!!!"); }}Copy the code
7. Post final notification
@param joinPoint */ @after (value = POINT_CUT) public void param joinPoint */ @param joinPoint (value = POINT_CUT) public void param joinPoint (value = POINT_CUTdoAfterAdvice(JoinPoint joinPoint){
logger.info("Post final notification executed!!!!");
}Copy the code
8. Surround notifications
/** * Wrap notification: * Wrap notification is very powerful and can determine if and when the target method executes, if method parameters need to be replaced during execution, and if return values need to be replaced after execution. * Around the notice the first parameter must be org. Aspectj. Lang. ProceedingJoinPoint type * / @ Around (value = POINT_CUT) public ObjectdoAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
logger.info("Target method name for surround notification:"+proceedingJoinPoint.getSignature().getName());
try {
Object obj = proceedingJoinPoint.proceed();
return obj;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}Copy the code