Home  >  Article  >  Java  >  How SpringBoot uses AOP to record interface operation logs

How SpringBoot uses AOP to record interface operation logs

PHPz
PHPzforward
2023-05-11 16:58:141397browse

1. Introduction to AOP

1. What is AOP

AOP: Aspect Oriented Programming

AOP focuses not on A certain class or certain methods; control a large number of resources and focus on a large number of classes and methods.

2.AOP application scenarios and common terms

  • Permission control, cache control, transaction control, distributed tracing, exception handling Wait

  • Target: target class, that is, the class that needs to be proxied. For example: UserService

  • Joinpoint: The so-called connection point refers to those methods that may be intercepted. For example: All methods

  • PointCut Pointcut: A connection point that has been enhanced. For example: addUser()

  • Advice notification/enhancement, enhance the code. For example: after, before

  • Weaving (weaving): refers to the process of applying enhanced advice to the target object target to create a new proxy object proxy.

  • Aspect: It is the combination of pointcut and notification advice

3. Characteristics of AOP

1) Reduce the coupling between modules and improve the aggregation of business code. (High cohesion and low coupling)

2) Improved code reusability

3) Improved system scalability. (Higher versions are compatible with lower versions)

4) New functions can be added without affecting the original functions

2. SpringBoot uses AOP to implement the process

1.Introduce dependencies

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

2.Encapsulate the logging entity class

@Getter
@Setter
@ApiModel(value = "Systemlog对象", description = "")
public class Systemlog implements Serializable {

    private static final long serialVersionUID = 1L;

      @ApiModelProperty("ID")
      @TableId(value = "id", type = IdType.AUTO)
      private Integer id;

      @ApiModelProperty("用户名")
      private String userName;

      @ApiModelProperty("用户ID")
      private Integer userId;

      @ApiModelProperty("操作描述")
      private String operate;

      @ApiModelProperty("模块")
      private String module;

      @ApiModelProperty("创建日志时间")
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
      private Date createTime;

      @ApiModelProperty("操作结果")
      private String result;

}

3.Write annotation classes(custom log Annotation class)

/**
 * controller层切面日志注解
 * @author hsq
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemControllerLog {
    // 操作描述
    String operate();
    // 模块
    String module();
}

4. Write the aspect class of the operation log

**
 * @author hsq
 */
@Aspect
@Component
public class SystemLogAspect {

    private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

    @Autowired
    private ISystemlogService iSystemlogService;

    @Autowired
    private UserService userService;

    /**
     * Controller层切点
     */
    @Pointcut("@annotation(com.hsq.demo.config.SystemControllerLog)")
    public void SystemControllerLog(){

    }
    
   
    /**
     * 前置通知 用于拦截Controller层记录用户的操作的开始时间
     * @param joinPoint 切点
     * @throws InterruptedException
     */
    @Before("SystemControllerLog()")
    public void doBefore(JoinPoint joinPoint) throws InterruptedException{
        logger.info("进入日志切面前置通知!");

    }

    @After("SystemControllerLog()")
    public void doAfter(JoinPoint joinPoint) {
        logger.info("进入日志切面后置通知!");

    }

    /**value切入点位置
     * returning 自定义的变量,标识目标方法的返回值,自定义变量名必须和通知方法的形参一样
     * 特点:在目标方法之后执行的,能够获取到目标方法的返回值,可以根据这个返回值做不同的处理
     */
    @AfterReturning(value = "SystemControllerLog()", returning = "ret")
    public void doAfterReturning(Object ret) throws Throwable {
    }

    /***
     * 异常通知 记录操作报错日志
     * * @param joinPoint
     * * @param e
     * */
    @AfterThrowing(pointcut = "SystemControllerLog()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        logger.info("进入日志切面异常通知!!");
        logger.info("异常信息:" + e.getMessage());
    }
        
        
    //使用这个方法先注释前面三个方法,留before方法就行
    /**
     * 通知包裹了目标方法,在目标方法调用之前和之后执行自定义的行为
     * ProceedingJoinPoint切入点可以获取切入点方法上的名字、参数、注解和对象
     * @param joinPoint
     */
    @Around("SystemControllerLog() && @annotation(systemControllerLog)")
  public Result doAfterReturning(ProceedingJoinPoint joinPoint, SystemControllerLog systemControllerLog) throws Throwable {
        logger.info("设置日志信息存储到表中!");
        //joinPoint.proceed() 结果集
        //参数数组
        Object[] args = joinPoint.getArgs();
        //请求参数数据
        String requestJson = JSONUtil.toJsonStr(args);
        //方法名
        String methodName = joinPoint.getSignature().getName();
        //得到request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //得到token
        String token = request.getHeader("token");
        String userId = JWT.decode(token).getAudience().get(0);
        User user = userService.getById(userId);
        logger.info("得到用户信息:"+user.toString());
        //写入数据库操作日志
        Systemlog systemlog = new Systemlog();
        systemlog.setUserId(user.getUid());
        systemlog.setUserName(user.getUname());
        systemlog.setOperate(systemControllerLog.operate());
        systemlog.setModule(systemControllerLog.module());
        systemlog.setCreateTime(new Date());
        //存入返回的结果集 joinPoint.proceed()
        Result proceed = (Result) joinPoint.proceed();
        systemlog.setResult(JSONUtil.toJsonStr(joinPoint.proceed()));
        //保存
       saveSystemLog(systemlog);

       return proceed;

    }

}

5.Use the controller

 @GetMapping("/userListPage")
 @SystemControllerLog(operate = "用户查询",module = "用户管理")
 public Result findUserList( @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam String username,
                                @RequestParam String loveValue,
                                @RequestParam String address) {}
  @PostMapping("/addOrUpdate")
  @SystemControllerLog(operate = "用户修改或者添加",module = "用户管理")
  public Result addOrUpdateUser(@RequestBody User user){}

6. Database records

How SpringBoot uses AOP to record interface operation logs

The above is the detailed content of How SpringBoot uses AOP to record interface operation logs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete