Spring框架 基于注解的 AOP 实现

概述

Spring AOP 是基于动态代理模式实现,采用两种,JDK动态代理、CGLIB的动态代理。

使用 JDK 的 Proxy 实现代理,要求目标类与代理类实现相同的接口。若目标类不存在接口,则无法使用该方式实现。对于无接口的类,要为其创建动态代理,就要使用 CGLIB 来实现。
CGLIB 代理的生成原理是生成目标类的子类,而子类是增强过的,这个子类对象就是代理对象。所以使用 CGLIB 生成动态代理,要求目标类必须能够被继承,即不能是 final 的类。

AspectJ是Eclipse的一个开源项目。对于AOP这种编程思想进行了实现,支持注解式开发。Spring引入了这个开源项目的实现,而在 Spring 中使用 AOP 开发时,一般使用 AspectJ 的实现方式。

AspectJ 中常用的通知有五种类型:
(1)前置通知
(2)后置通知
(3)环绕通知
(4)异常通知
(5)最终通知

AspectJ 采用 “execution(方法签名)” 指定切入点。
举例如下
execution(public * (…))
指定切入点为:任意公共方法。
execution(
set*(…))
指定切入点为:任何一个以“set”开始的方法。
execution(* com.service..(…))
指定切入点为:定义在 service 包里的任意类的任意方法。
execution(* com.service….(…))
指定切入点为:定义在 service 包或者子包里的任意类的任意方法。“…”出现在类名中时,后
面必须跟“”,表示包、子包下的所有类。
execution(
…service..*(…))
指定所有包下的 serivce 子包下所有类(接口)中所有方法为切入

AspectJ 基于注解的 AOP 实现

1. @Aspect 注解注释的Class被标识为切面类

@Aspect
public class AspectCheck {
}

2. @Before前置通知 方法签名有 JoinPoint 参数

    @Before("execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))")
    public void beforeCheck(){
        System.out.println("前置通知,执行日志记录");
    }

3. @AfterReturning 后置通知,@AfterReturning注解有 returning 属性,可以在切面方法结束后,返回结果。最好定义为Object

    @AfterReturning(value = "execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))",returning = "result")
    public void afterReturning(Object result){
        if (result != null){
            String s = (String) result;
            result = s.toUpperCase();
        }
        System.out.println("后置通知:" + result);
    }

4. @Around 环绕通知,被增强的方法有 ProceedingJoinPoint 参数

    @Around(value = "execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = null;
        //业务功能执前的增强功能
        System.out.println("环绕通知,增强功能。执行业务逻辑前执行,如打印日志");
        //业务方法调用
        proceed = proceedingJoinPoint.proceed();
        //业务功能执行后的增强功能
        System.out.println("环绕通知,增强功能。执行业务逻辑后执行,如事务处理");
        //返回业务方法结果
        return proceed;
    }

5. @AfterThrowing 异常通知,注解中有 throwing 属性。在目标方法抛出异常后执行。该注解的 throwing 属性用于指定所发生的异常类对象

    @AfterThrowing(value = "execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))",throwing = "throwable")
    public void afterThrowing(Throwable throwable){
        //发生异常时,打印报错日志。通知开发人员等操作
        System.out.println("异常通知:" + throwable.getMessage());
    }

6. @After 最终通知,无论目标方法是否抛出异常,该增强均会被执行。个人理解为try catch finally里的finally一样

    @After("execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))")
    public void after(){
        System.out.println("最终通知:方法已结束");
    }

7. @Pointcut 定义切入点,如下代码里,after2方法可用@Pointcut注解的方法名。来注入execution属性

    @After("execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))")
    public void after(){
        System.out.println("最终通知:方法已结束");
    }
    
    @Pointcut("execution(* com.seckill.service.impl.GoodsServiceImpl.secKill(..))")
    private void pointcut(){
        
    }

    @After("pointcut()")
    public void after2(){
        System.out.println("最终通知:方法已结束");
    }

个人学习总结,难免有所纰漏,请自行斟酌正确性。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值