一篇文章帮你学会 Spring 基于注解的AOP配置

一、注解使用

@EnableAspectJAutoProxy 相当于在bean.xml 中配置开启注解 AOP 的支持

<!-- 配置spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

切入点的设置表明了对哪些方法进行增强

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
@Configuration
@ComponentScan("spring")
//不使用XML的配置方式
@EnableAspectJAutoProxy
//声明当前类为切面类
@Aspect
public class Logger {
    @Pointcut("execution(* spring.service.impl.*.*(..))")
    private void pt(){}

    /**
     * 前置通知
     */
    @Before("pt()")
    public void beforePrintLog() {
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }

    /**
     * 后置通知
     */
    @AfterReturning("pt()")
    public void afterReturningPrintLog() {
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }

    /**
     * 异常通知
     */
    @AfterThrowing("pt()")
    public void afterThrowingPrintLog() {
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }

    /**
     * 最终通知
     */
    @After("pt()")
    public void afterPrintLog() {
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
}

二、环绕通知

不过注解使用时,会存在一个问题,就是最终通知和后置通知的顺序会搞反
在这里插入图片描述
解决办法就是换为环绕通知,也就是自己定义

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
@Configuration
@ComponentScan("spring")
//不使用XML的配置方式
@EnableAspectJAutoProxy
//声明当前类为切面类
@Aspect
public class Logger {
    @Pointcut("execution(* spring.service.impl.*.*(..))")
    private void pt(){}
    /**
     * 环绕通知
     *
     * @param pjp
     * @return
     */
    @Around("pt()")
    public Object aroundPringLog(ProceedingJoinPoint pjp) {
        Object rtValue = null;
        try {
            //得到方法执行所需的参数
            Object[] args = pjp.getArgs();
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
            //明确调用业务层方法(切入点方法)
            rtValue = pjp.proceed(args);

            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");

            return rtValue;
        } catch (Throwable t) {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        } finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
}

在这里插入图片描述

三、多个增强类对同一个方法增强,设置增强类优先级

在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高

@Component
@Aspect
@Order(1)
public class Logger 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南淮北安

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

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

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

打赏作者

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

抵扣说明:

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

余额充值