Spring Boot implements AOP with annotations

Step 1: add dependency

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

Step 2: define the tangent class

package com.zdst.scs.web.controller.interact.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * The function of this tangent class is to add parameters to the Model of the method
* The tangent expression matches two. If only one expression can be removed, the matching condition becomes one
*/ @Aspect @Component public class  aop {
  //The meaning of pointcut expression is different from that of all methods in a specified class
  //Specify to method: public * com.web.controller.UserController.aop * (..)) UserControllerIn class aop Method at the beginning
// Specify to class: public * com.web.controller.UserController.*.*(..)) UserControllerAll methods in class
@Pointcut(
"execution(public * com.web.controller.UserController.aop*(..)) || " + "execution(public * com.web.controller.AnotherController.aop*(..))") public void token(){} /** * Add token */ @Before("token()") public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest();//Obtain request request HttpSession session = (HttpSession)request.getSession();//Obtain session String token=(String)session.getAttribute("token");//Obtain session Stored property values Object[] obj = joinPoint.getArgs();//Get target method parameter information for (Object argItem : obj) { if (argItem instanceof Model) { Model model = (Model) argItem; model.addAttribute("token",token);//Set parameter content } } } //After the target method is executed @After("token()") public void doAfter(){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); System.out.println(request.getAttributeNames()); }   //Execute after getting the return data of the target method @AfterReturning(returning = "object",pointcut = "token()") public void doAfterReturn(Object object){ System.out.println(object.toString()); } }

When the method to be enhanced is called in the pointcut expression, it will first enter the doBefore method for enhancement

Define pointcut expression execution(* com.sample.service.impl.. *. * (..))
The syntax is as follows:
The whole expression can be divided into five parts:
(1). execution(): expression body.
(2) the first * sign indicates the return type, and the * sign indicates all types.
(3) package name: indicates the package name to be intercepted. The next two periods indicate the current package and all subpackages of the current package. com.sample.service.impl package and methods of all classes under the descendant package.
(4) the second * indicates the class name, and * indicates all classes.
(5), * (..): the last asterisk indicates the method name, * indicates all methods, followed by parentheses indicating the parameters of the method, and two periods indicating any parameters.

To learn more, please click Baidu pointcut expression

Tags: Java Session Spring Asterisk

Posted on Mon, 02 Dec 2019 00:00:21 -0800 by adders