javaspringboot--AOP

AOP

AOP:Aspect Oriented Programming (面向切面编程、面向方面编程),其实就是面向特定方法编程
在这里插入图片描述
依赖

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>
@Component
@Aspect
@Slf4j
public class TimeAspect {
    @Around("execution(* com.example.demo.service.*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long begin = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature()+"用时:{}",end-begin);
        return  result;
    }
}

AOP核心概念

在这里插入图片描述

AOP执行流程

在这里插入图片描述

通知类型

在这里插入图片描述
切入点表达式

    @Pointcut("execution(* com.example.demo.service.*.*(..))")
    private void pt(){}
    
    @Around("pt()")

通知执行顺序

在这里插入图片描述
类名靠前先执行
也可以用@Order(数字)来控制,数字越小越先执行。

切入点表达式

  • execution(…):根据方法的签名来匹配
  • @annotation(…):根据注解匹配
切入点表达式-execution

在这里插入图片描述
通配符
在这里插入图片描述
切入点表达式书写建议

  • 所有业务方法名在命名时尽量规范,方便切入点表达式快速匹配。如:查询类方法都是 find 开头,更新类方法都是 update开头。
  • 描述切入点方法通常基于接口描述而不是直接描述实现类增强拓展性
  • 在满足业务需要的前提下,尽量缩小切入点的匹配范围。如: 包名匹配尽量不使用…,使用*匹配单个包
切入点表达式-annotation

在这里插入图片描述

@Retention(RetentionPolicy.RUNTIME)//运行时
@Target(ElementType.METHOD)//方法上
public @interface MyLog {

}

 @Pointcut("@annotation(com.example.demo.aop.MyLog)")

连接点

  • 在Spring中用JoinPoint抽象了连接点,用它可以获得方法执行时的相关信息,如目标类名、方法名、方法参数等
  1. 对于 @Around 通知,获取连接点信息只能使用 ProceedingJoinPoint
  2. 对于其他四种通知,获取连接点信息只能使用 JoinPoint,它是 ProceedingJoinPoint 的父类型
 		//获取目标对象类名
        String name = joinPoint.getTarget().getClass().getName();
        log.info("1:{}",name);
        //获取目标对象的方法名
        String name1 = joinPoint.getSignature().getName();
        log.info("2:{}",name1);
        //获取目标对象运行时传入的参数
        Object[] args = joinPoint.getArgs();
        log.info("3:{}", Arrays.toString(args));
        //放行目标方法的执行
        Object result = joinPoint.proceed();
        //获取目标方法运行时的返回值

切面 类获取网络请求jwt令牌

@Component
@Aspect
@Slf4j
public class TimeAspect {

    @Autowired
    HttpServletRequest request;
    
    @Pointcut("@annotation(com.example.demo.aop.MyLog)")
    private void pt(){}

    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJwt(jwt);
        Object result = joinPoint.proceed();
        return  result;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值