时隔1年,我终于弄懂了Java 中的 AOP操作

1. AOP概述

2. AOP快速入门 

依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

 示例:记录方法的执行耗时

创建一个aop类:(在执行com.etc.aoptest.service.impl.UserServiceImpl类下否方法时就会进行环绕通知)

package com.etc.aoptest.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class TimeAspect {

    @Around("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable{

        long begin = System.currentTimeMillis();

        Object result = joinPoint.proceed();

        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature() + "方法执行耗时: {}ms",end - begin);

        return result;
    }

}

3. AOP的核心概念

4. AOP通知类型

创建一个aop类用于测试AOP通知类型

package com.etc.aoptest.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class AspectType {

    //环绕通知,目标方法前后都被执行
    @Around("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("方法执行前..................................");

        Object result = joinPoint.proceed();

        log.info("方法执行后..................................");
    }

    //环绕前通知
    @Before("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    public void before() {
        log.info("before通知..................................");
    }

    //环绕后通知
    @After("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    public void after() {
        log.info("after通知..................................");
    }

    //返回后通知
    @AfterReturning("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    public void afterReturning() {
        log.info("afterReturning通知..................................");
    }

    //异常后通知
    @AfterThrowing("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    public void afterThrowing(){
        log.info("afterThrowing通知..................................");
    }

}

可以通过@Pointcut简化冗余切面点表达式

修改后如下所示:

package com.etc.aoptest.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class AspectType {

    @Pointcut("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    private void pt(){};

    //环绕通知,目标方法前后都被执行
    //@Around("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    @Around("pt()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("方法执行前..................................");

        Object result = joinPoint.proceed();

        log.info("方法执行后..................................");
    }

    //环绕前通知
    //@Before("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    @Before("pt()")
    public void before() {
        log.info("before通知..................................");
    }

    //环绕后通知
    //@After("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    @After("pt()")
    public void after() {
        log.info("after通知..................................");
    }

    //返回后通知
    //@AfterReturning("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    @AfterReturning("pt()")
    public void afterReturning() {
        log.info("afterReturning通知..................................");
    }

    //异常后通知
    //@AfterThrowing("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..)) ")
    @AfterThrowing("pt()")
    public void afterThrowing(){
        log.info("afterThrowing通知..................................");
    }

}

其他aop类也可以引入当前aop类的切面点表达式

不过需要将private void pt()修改为public void pt();

示例:

package com.etc.aoptest.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class TimeAspect {

    //@Around("execution(* com.etc.aoptest.service.impl.UserServiceImpl.*(..))")
    @Around("com.etc.aoptest.aop.AspectType.pt()")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable{

        long begin = System.currentTimeMillis();

        Object result = joinPoint.proceed();

        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature() + "方法执行耗时: {}ms",end - begin);

        return result;
    }

}

5. AOP执行顺序

6. AOP切入点表达式

7.  AOP多个切入点表达式 @Annotation

 

这样就无需写表达式,只需要写在对应的方法上加上自定义的注解,这时Annotation就会通过对应的注解切入点表达式。 

示例:

步骤一

首先在一个log包内创建UserLog自定义注解

UserLog:

package com.etc.aoptest.log;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME) //运行时生效
@Target(ElementType.METHOD) //标识方法
public @interface UserLog {

}

步骤二

aop包下创建AnnoAspect 用于AOP注入时实现的环绕通知

package com.etc.aoptest.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class AnnoAspect {

    @Pointcut("@annotation(com.etc.aoptest.log.UserLog)")
    private void pt(){}


    @Around("pt()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("UserLog-方法执行前..................................");

        Object result = joinPoint.proceed();

        log.info("UserLog-方法执行后..................................");
        return result;
    }

}

步骤三 

在所注入AOP的方法上加上自定义注解名@UserLog

package com.etc.aoptest.controller;


import com.etc.aoptest.log.UserLog;
import com.etc.aoptest.pojo.User;
import com.etc.aoptest.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

   private final IUserService userService;

   @UserLog
   @RequestMapping("/getList")
   public List<User> getList(){
       return userService.getList();
   }

   @UserLog
   @RequestMapping("/{id}")
    public User getById(@PathVariable Long id){
       return userService.getById(id);
   }

}

8. AOP连接点

通过JoinPoint来获取方法执行时的相关信息,只有环绕通知使用ProceedingJoinPoint来获取相关信息而另外四种AOP通知类型则通过JoinPoint来获取相关信息。

 9. AOP实战演练(操作日志表)

要求:实现操作日志表,记录对数据库的增删改查操作的日志。

步骤一

先创建一个记录日志表实体并创建该数据库

pojo.OperateLog:
package com.etc.aoptest.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@TableName("tb_operate_log")
public class OperateLog {

    @TableId(type = IdType.AUTO)
    private Long id;

    //操作者ID
    @TableField("operate_Id")
    private Long operateId;

    //类名
    @TableField("class_name")
    private String className;

    //方法名
    @TableField("method_name")
    private String methodName;

    //方法参数
    @TableField("method_params")
    private String methodParams;

    //返回结果
    @TableField("return_value")
    private String returnValue;

    //操作耗时
    @TableField("cost_time")
    private Long costTime;

    //操作时间
    @TableField("operate_time")
    private LocalDateTime operateTime;
}

anno.Log:

package com.etc.aoptest.log;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
}

 步骤二

编写AOP操作,用于在执行该方法时进行环绕通知操作,使用ProceedingJoinPoint获取执行方法的相关信息存入OperateLog数据库中

package com.etc.aoptest.aop;

import com.alibaba.fastjson.JSONObject;
import com.etc.aoptest.mapper.OperateLogMapper;
import com.etc.aoptest.pojo.OperateLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@Aspect
@RequiredArgsConstructor
@Slf4j
public class LogAspect {

    private final OperateLogMapper operateLogMapper;

    @Around("@annotation(com.etc.aoptest.log.Log)")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable{

        //TODO 1.通过HttpServletRequest获取token值通过JWT解析获取当前用户id 或者 2.通过ThreadLocal获取当前用户id
        Long operateId = 1L;

        //类名
        String className = joinPoint.getTarget().getClass().getName();

        //方法名
        String methodName = joinPoint.getSignature().getName();


        Object[] args = joinPoint.getArgs();
        //方法参数
        String methodParams = Arrays.toString(args);

        Long begin = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        Long end = System.currentTimeMillis();

        //方法返回值
        String returnValue = JSONObject.toJSONString(result);

        //操作耗时
        Long costTime = end - begin;
        OperateLog operateLog = new OperateLog(null,operateId,className,methodName,methodParams,returnValue,costTime,LocalDateTime.now());
        operateLogMapper.insert(operateLog);

        log.info("AOP记录日志操作:{}",operateLog);

        return result;
    }
}

步骤三

 对于要进行AOP切入的方法前加上自定义的@Log注解

package com.etc.aoptest.controller;


import com.etc.aoptest.log.Log;
import com.etc.aoptest.pojo.User;
import com.etc.aoptest.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

   private final IUserService userService;

   @Log
   @RequestMapping("/getList")
   public List<User> getList(){
       return userService.getList();
   }

   @Log
   @RequestMapping("/{id}")
    public User getById(@PathVariable Long id){
       return userService.getById(id);
   }

}

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

探索星辰大海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值