SpringBoot AOP切面编程精讲:实现方式、Spring区别及与自定义注解生产实战
文章目录一、AOP核心简介二、SpringBoot快速实现AOP注解版1. 引入核心依赖2. 编写切面类五种通知完整示例3. 测试接口4. 通知执行顺序三、传统Spring AOP vs SpringBoot AOP核心区别1. 核心差异汇总四、SpringBoot AOP切面 vs 自定义注解生产重点1. 核心关系2. 两种方式对比五、实战1纯AOP全局拦截仅演示不推荐生产六、实战2AOP自定义注解生产标准写法1. 创建自定义标记注解2. 切面拦截该注解3. 业务接口按需使用七、最终生产总结一、AOP核心简介AOP面向切面编程是Spring两大核心之一核心作用就是不改动业务代码就能对方法做统一增强实现业务逻辑与公共逻辑解耦。常用于统一日志、接口耗时统计、权限校验、事务管理、操作记录等场景。核心四要素切点要拦截哪些方法、切面增强处理类、通知前后/环绕/异常等增强逻辑、连接点被拦截的目标方法。二、SpringBoot快速实现AOP注解版1. 引入核心依赖SpringBoot无需繁琐配置仅需引入AOP启动器自动完成装配。!-- SpringBoot AOP核心依赖 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependency2. 编写切面类五种通知完整示例Aspect标记切面类Component交给容器管理通过切点匹配目标方法配置不同通知完成增强。importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.stereotype.Component;AspectComponentpublicclassLogAndTimeAspect{// 切点拦截controller包下所有方法Pointcut(execution(* com.example.demo.controller.*.*(..)))publicvoidcontrollerPointCut(){}// 前置通知方法执行前Before(controllerPointCut())publicvoidbeforeAdvice(){System.out.println(【前置通知】方法开始执行);}// 后置通知无论是否异常执行完毕都执行After(controllerPointCut())publicvoidafterAdvice(){System.out.println(【后置通知】方法执行结束);}// 返回通知方法正常返回后执行AfterReturning(pointcutcontrollerPointCut(),returningresult)publicvoidafterReturningAdvice(Objectresult){System.out.println(【返回通知】返回值result);}// 异常通知方法报错时执行AfterThrowing(pointcutcontrollerPointCut(),throwinge)publicvoidafterThrowingAdvice(Exceptione){System.out.println(【异常通知】异常信息e.getMessage());}// 环绕通知功能最强手动控制方法执行前后逻辑Around(controllerPointCut())publicObjectaroundAdvice(ProceedingJoinPointjoinPoint)throwsThrowable{longstartTimeSystem.currentTimeMillis();// 执行目标方法ObjectresultjoinPoint.proceed();longendTimeSystem.currentTimeMillis();System.out.println(【环绕通知】方法耗时(endTime-startTime)ms);returnresult;}}3. 测试接口RestControllerpublicclassAopTestController{GetMapping(/aop/test)publicStringaopTest(){System.out.println(业务方法执行中);returnAOP执行成功;}}4. 通知执行顺序正常执行环绕前置 → 前置通知 → 目标方法 → 返回通知 → 后置通知 → 环绕后置。异常执行环绕前置 → 前置通知 → 方法报错 → 异常通知 → 后置通知。三、传统Spring AOP vs SpringBoot AOP核心区别两者底层原理、切点规则、通知逻辑完全一样核心区别仅在配置和使用方式。1. 核心差异汇总配置方式Spring AOP必须写XML配置繁琐冗余SpringBoot AOP零XML纯注解开箱即用。依赖管理Spring需手动导入多个Jar、处理版本冲突SpringBoot只需一个启动器自动管控版本。代理开启Spring需手动配置AOP自动代理标签SpringBoot默认自动开启无需手动配置。适用场景Spring AOP仅用于老旧SSM项目维护SpringBoot AOP是新项目、微服务标配。四、SpringBoot AOP切面 vs 自定义注解生产重点1. 核心关系AOP切面干活的拦截器负责方法增强逻辑自定义注解标记开关只负责筛选哪些方法需要拦截。两者不是二选一生产标配AOP切面 自定义注解组合使用。2. 两种方式对比对比维度纯AOP按包名拦截executionAOP自定义注解精准拦截拦截范围全局批量拦截范围大只拦截加注解的方法精准可控误拦截风险高容易拦截无关方法无不加注解不生效适用场景全局耗时、基础统一日志操作日志、权限、限流、数据脱敏生产推荐少用、不推荐首选、企业标准五、实战1纯AOP全局拦截仅演示不推荐生产按包名一刀切所有接口都拦截无法单独控制容易产生垃圾日志。AspectComponentpublicclassAllApiLogAspect{Pointcut(execution(* com.example.demo.controller.*.*(..)))publicvoidpointcut(){}Around(pointcut())publicObjectaround(ProceedingJoinPointjoinPoint)throwsThrowable{System.out.println(接口入参Arrays.toString(joinPoint.getArgs()));ObjectresultjoinPoint.proceed();System.out.println(接口出参result);returnresult;}}六、实战2AOP自定义注解生产标准写法1. 创建自定义标记注解importjava.lang.annotation.*;// 仅标记方法、运行时生效Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)DocumentedpublicinterfaceOperateLog{Stringvalue()default;// 记录操作描述}2. 切面拦截该注解AspectComponentpublicclassOperateLogAspect{// 只拦截加了OperateLog注解的方法Pointcut(annotation(com.example.demo.annotation.OperateLog))publicvoidoperateLogPointcut(){}Around(operateLogPointcut())publicObjectaround(ProceedingJoinPointjoinPoint)throwsThrowable{System.out.println( 记录重要操作日志 );System.out.println(操作方法joinPoint.getSignature().getName());System.out.println(请求参数Arrays.toString(joinPoint.getArgs()));ObjectresultjoinPoint.proceed();System.out.println(操作返回结果result);returnresult;}}3. 业务接口按需使用RestControllerpublicclassUserController{// 普通查询不加注解 → 不记录日志GetMapping(/user/get)publicStringgetUser(){return查询用户;}// 重要操作加注解 → AOP自动记录日志OperateLog(删除用户操作)GetMapping(/user/delete)publicStringdeleteUser(){return删除成功;}}七、最终生产总结简单全局通用功能可少量使用纯AOP包名拦截业务特殊增强功能一律使用AOP自定义注解精准可控、好维护SpringBoot新项目只用注解AOP传统Spring XML配置仅作老旧项目维护使用。