Java SpringAOP入门

AOP入门

AOP:Aspect Oriented Programming(面向切面编程、面向方面编程),其实说白了,面向切面编程就是面向特定方法编程。

在我们想给某些方法都加一段功能相同的代码时,不必修改每个业务代码,可以通过AOP来使用一段代码实现

AOP的四个主要优势

  • 减少重复代码:不需要在业务方法中定义大量的重复性的代码,只需要将重复性的代码抽取到AOP程序中即可。
  • 代码无侵入:在基于AOP实现这些业务功能时,对原有的业务代码是没有任何侵入的,不需要修改任何的业务代码。
  • 提高开发效率
  • 维护方便

实现依赖

java 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

小案例

java 复制代码
@Component
@Aspect //声明当前类为切面类
@Slf4j
public class RecordTimeAspect {

    @Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        //记录方法执行开始时间
        long begin = System.currentTimeMillis();

        //执行原始方法
        Object result = pjp.proceed();

        //记录方法执行结束时间
        long end = System.currentTimeMillis();

        //计算方法执行耗时
        log.info("方法执行耗时: {}毫秒",end-begin);
        return result;
    }
}

AOP核心概念

连接点:JoinPoint,可以被AOP控制的方法

通知:Advice,重复的逻辑,也就是共性功能,一般也就是定义的整个方法

切入点:PointCut ,匹配连接点的条件,通知仅会在切入点方法执行时被应用,就是切入点表达式

通知类型

@Around 环绕通知,此注解标注的通知方法在目标方法前、后都被执行
@Before 前置通知,此注解标注的通知方法在目标方法前被执行
@After 后置通知,此注解标注的通知方法在目标方法后被执行,无论是否有异常都会执行
@AfterReturning 返回后通知,此注解标注的通知方法在目标方法后被执行,有异常不会执行
@AfterThrowing 异常后通知,此注解标注的通知方法发生异常后执行

@Around:需要自己调用proceed()让目标方法执行,且通知方法返回值必须为Object

java 复制代码
@Slf4j
@Component
@Aspect
public class MyAspect1 {
    //前置通知
    @Before("execution(* com.itheima.service.*.*(..))")
    public void before(JoinPoint joinPoint){
        log.info("before ...");

    }

    //环绕通知
    @Around("execution(* com.itheima.service.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("around before ...");

        //调用目标对象的原始方法执行
        Object result = proceedingJoinPoint.proceed();
        
        //原始方法如果执行时有异常,环绕通知中的后置代码不会在执行了
        
        log.info("around after ...");
        return result;
    }

    //后置通知
    @After("execution(* com.itheima.service.*.*(..))")
    public void after(JoinPoint joinPoint){
        log.info("after ...");
    }

    //返回后通知(程序在正常执行的情况下,会执行的后置通知)
    @AfterReturning("execution(* com.itheima.service.*.*(..))")
    public void afterReturning(JoinPoint joinPoint){
        log.info("afterReturning ...");
    }

    //异常通知(程序在出现异常的情况下,执行的后置通知)
    @AfterThrowing("execution(* com.itheima.service.*.*(..))")
    public void afterThrowing(JoinPoint joinPoint){
        log.info("afterThrowing ...");
    }
}

切入点表达式

java 复制代码
//execution(访问修饰符?  返回值  包名.类名.?方法名(方法参数) throws 异常?)
@Pointcut("execution(* com.itheima.service.*.*(..))")//service下所有类所有方法
private void pt(){}
之后就可以在通知注解后引用切入点
@Before("pt()")
//@Before("execution(* com.itheima.service.*.*(..))")
//两种写法等同
  • * :单个独立的任意符号,可以通配任意返回值、包名、类名、方法名、任意类型的一个参数,也可以通配包、类、方法名的一部分
  • .. :多个连续的任意符号,可以通配任意层级的包,或任意类型、任意个数的参数
相关推荐
28岁青春痘老男孩12 小时前
JDK8+SpringBoot2.x 升级 JDK 17 + Spring Boot 3.x
java·spring boot
方璧12 小时前
限流的算法
java·开发语言
元Y亨H12 小时前
Nacos - 服务注册
java·微服务
Hi_kenyon12 小时前
VUE3套用组件库快速开发(以Element Plus为例)二
开发语言·前端·javascript·vue.js
曲莫终12 小时前
Java VarHandle全面详解:从入门到精通
java·开发语言
一心赚狗粮的宇叔13 小时前
中级软件开发工程师2025年度总结
java·大数据·oracle·c#
奋进的芋圆13 小时前
DataSyncManager 详解与 Spring Boot 迁移指南
java·spring boot·后端
ghie909013 小时前
基于MATLAB GUI的伏安法测电阻实现方案
开发语言·matlab·电阻
Gao_xu_sheng13 小时前
Inno Setup(专业安装/更新 EXE)
开发语言