news 2026/2/21 3:42:37

AOP代码层面的具体使用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
AOP代码层面的具体使用

现在看具体实现。

一、AOP 到底对什么使用?

AOP主要针对横切关注点,这些代码通常:

  1. 分散在各处:相同的逻辑出现在多个地方
  2. 与业务逻辑无关:不是核心业务,但又必须存在
  3. 容易忘记或重复:如每个方法都要加日志

典型应用场景:

// 没有AOP时,这些代码会重复出现在很多方法中publicclassService{publicvoidcreateOrder(Orderorder){// 1. 记录日志(重复代码)log.info("开始创建订单: {}",order);// 2. 权限检查(重复代码)if(!hasPermission()){thrownewSecurityException();}// 3. 性能监控开始(重复代码)longstart=System.currentTimeMillis();// ---------- 核心业务逻辑 ----------// 验证订单// 计算价格// 保存到数据库// 发送通知// ---------------------------------// 4. 性能监控结束(重复代码)longcost=System.currentTimeMillis()-start;log.info("方法执行耗时: {}ms",cost);// 5. 再次记录日志(重复代码)log.info("订单创建成功: {}",order.getId());}// 其他方法也有同样的重复代码...}

二、代码层面怎么使用?

1.Spring AOP 注解方式(最常用)

// 1. 定义切面@Aspect@ComponentpublicclassLoggingAspect{// 2. 定义切入点:哪些方法需要被增强@Pointcut("execution(* com.example.service.*.*(..))")publicvoidserviceMethods(){}// 3. 前置通知:在方法执行前执行@Before("serviceMethods()")publicvoidlogBefore(JoinPointjoinPoint){StringmethodName=joinPoint.getSignature().getName();Object[]args=joinPoint.getArgs();log.info("方法 {} 开始执行,参数: {}",methodName,Arrays.toString(args));}// 4. 后置通知:在方法执行后执行@AfterReturning(pointcut="serviceMethods()",returning="result")publicvoidlogAfterReturning(JoinPointjoinPoint,Objectresult){log.info("方法 {} 执行成功,返回值: {}",joinPoint.getSignature().getName(),result);}// 5. 异常通知:方法抛出异常时执行@AfterThrowing(pointcut="serviceMethods()",throwing="ex")publicvoidlogAfterThrowing(JoinPointjoinPoint,Exceptionex){log.error("方法 {} 执行异常: {}",joinPoint.getSignature().getName(),ex.getMessage());}// 6. 环绕通知:包裹整个方法@Around("serviceMethods()")publicObjectlogAround(ProceedingJoinPointjoinPoint)throwsThrowable{longstart=System.currentTimeMillis();try{// 执行原方法Objectresult=joinPoint.proceed();longcost=System.currentTimeMillis()-start;log.info("方法 {} 执行耗时: {}ms",joinPoint.getSignature().getName(),cost);returnresult;}catch(Exceptione){longcost=System.currentTimeMillis()-start;log.error("方法 {} 执行失败,耗时: {}ms",joinPoint.getSignature().getName(),cost);throwe;}}}

2.更实际的完整示例

// 业务服务类 - 保持干净,只关注核心逻辑@ServicepublicclassOrderService{// 没有日志、权限、事务等代码publicOrdercreateOrder(OrderRequestrequest){// 纯业务逻辑Orderorder=newOrder();order.setItems(request.getItems());order.calculateTotal();orderRepository.save(order);returnorder;}publicOrdergetOrder(Longid){returnorderRepository.findById(id).orElseThrow(()->newOrderNotFoundException(id));}}// 权限检查切面@Aspect@ComponentpublicclassSecurityAspect{@Before("@annotation(RequireRole)")// 只拦截有@RequireRole注解的方法publicvoidcheckPermission(JoinPointjoinPoint){MethodSignaturesignature=(MethodSignature)joinPoint.getSignature();RequireRoleannotation=signature.getMethod().getAnnotation(RequireRole.class);StringrequiredRole=annotation.value();if(!currentUser.hasRole(requiredRole)){thrownewAccessDeniedException("需要角色: "+requiredRole);}}}// 缓存切面@Aspect@ComponentpublicclassCacheAspect{@Around("@annotation(Cacheable)")publicObjectcacheResult(ProceedingJoinPointjoinPoint)throwsThrowable{StringcacheKey=generateCacheKey(joinPoint);// 先查缓存Objectcached=cache.get(cacheKey);if(cached!=null){returncached;}// 执行方法Objectresult=joinPoint.proceed();// 存入缓存cache.put(cacheKey,result,300);// 缓存5分钟returnresult;}}

3.自定义注解 + AOP(非常实用的模式)

// 自定义注解@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public@interfaceAuditLog{Stringaction();Stringmodule();}// 审计日志切面@Aspect@ComponentpublicclassAuditAspect{@AutowiredprivateAuditServiceauditService;@AfterReturning("@annotation(auditLog)")publicvoidauditSuccess(JoinPointjoinPoint,AuditLogauditLog){Useruser=SecurityContext.getCurrentUser();auditService.logSuccess(user.getId(),auditLog.module(),auditLog.action(),newDate());}@AfterThrowing(value="@annotation(auditLog)",throwing="ex")publicvoidauditFailure(JoinPointjoinPoint,AuditLogauditLog,Exceptionex){Useruser=SecurityContext.getCurrentUser();auditService.logFailure(user.getId(),auditLog.module(),auditLog.action(),ex.getMessage(),newDate());}}// 在业务方法上使用@ServicepublicclassUserService{@AuditLog(module="用户管理",action="创建用户")publicUsercreateUser(UserCreateRequestrequest){// 业务逻辑}@AuditLog(module="用户管理",action="删除用户")@RequireRole("ADMIN")// 可以组合多个切面publicvoiddeleteUser(LonguserId){// 业务逻辑}}

三、切入点表达式详解

@Pointcut("execution(* com.example.service.*.*(..))")// 1. execution: 最常用的表达式// * : 任意返回类型// com.example.service.* : service包下所有类// .* : 所有方法// (..) : 任意参数@Pointcut("@annotation(com.example.RequireRole)")// 2. 带有特定注解的方法@Pointcut("within(@org.springframework.stereotype.Service *)")// 3. 带有@Service注解的类中的所有方法@Pointcut("bean(orderService) || bean(userService)")// 4. 特定Bean中的方法@Pointcut("args(java.lang.String,..)")// 5. 第一个参数是String的方法

四、实际项目中的应用顺序

// 典型的切面执行顺序(可通过@Order控制)@Aspect@Order(1)// 最先执行publicclassSecurityAspect{// 1. 权限校验}@Aspect@Order(2)publicclassLoggingAspect{// 2. 日志记录}@Aspect@Order(3)publicclassTransactionAspect{// 3. 事务管理(Spring已有@Transactional)}@Aspect@Order(4)publicclassCacheAspect{// 4. 缓存处理}@Aspect@Order(5)publicclassRetryAspect{// 5. 重试机制@Around("@annotation(Retryable)")publicObjectretry(ProceedingJoinPointjoinPoint)throwsThrowable{intmaxAttempts=3;intattempt=0;while(attempt<maxAttempts){try{returnjoinPoint.proceed();}catch(Exceptione){attempt++;if(attempt==maxAttempts)throwe;Thread.sleep(1000*attempt);// 延迟重试}}returnnull;}}

五、最佳实践建议

  1. 保持切面简单:每个切面只做一件事
  2. 使用自定义注解:精确控制哪些方法需要增强
  3. 注意执行顺序:特别是安全、事务等有依赖关系的切面
  4. 性能考虑:切入点表达式要精确,避免匹配过多方法
  5. 异常处理:环绕通知要正确传递或转换异常

六、调试技巧

// 查看AOP是否生效@Aspect@ComponentpublicclassDebugAspect{@Before("execution(* com.example..*.*(..))")publicvoiddebug(JoinPointjoinPoint){System.out.println("AOP拦截: "+joinPoint.getTarget().getClass().getSimpleName()+"."+joinPoint.getSignature().getName());}}

AOP的核心价值在于:让关注点分离,让业务代码保持干净。当你发现某个非业务逻辑(日志、权限、缓存等)在多个地方重复出现时,就应该考虑使用AOP来解决了。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/5 13:12:11

Flink Hive 把 Hive 表变成“可流式消费”的数仓底座

1. Hive 在 Flink 里到底能干嘛 核心就两件事&#xff1a; 1&#xff09;读 Hive&#xff1a;既能一次性读&#xff08;bounded&#xff09;&#xff0c;也能像流一样追新增&#xff08;unbounded / streaming read&#xff09; 2&#xff09;写 Hive&#xff1a;批写支持 appe…

作者头像 李华
网站建设 2026/2/8 9:43:07

621-9937并行输入输出模块

621-9937 并行输入输出模块简介&#xff1a;621-9937 是工业自动化系统中的并行 I/O 模块可同时处理多个输入和输出信号支持数字量信号的快速采集与输出用于连接现场开关、传感器及执行设备输出信号可直接驱动继电器、指示灯等模块响应速度快&#xff0c;适合实时控制应用支持电…

作者头像 李华
网站建设 2026/2/20 17:28:36

​Android 基础入门教程​Handler消息传递机制浅析

3.3 Handler消息传递机制浅析 分类 Android 基础入门教程 本节引言 前两节中我们对Android中的两种事件处理机制进行了学习&#xff0c;关于响应的事件响应就这两种&#xff1b;本节给大家讲解的 是Activity中UI组件中的信息传递Handler&#xff0c;相信很多朋友都知道&…

作者头像 李华
网站建设 2026/2/14 22:18:19

HTML AI 编程助手AI

HTML AI 编程助手 AI 技术的飞速发展正在深刻改变开发者的工作方式。在 HTML 网页开发中&#xff0c;我们常常被大量细微却高频的重复操作降低效率。因此&#xff0c;AI 的出现可以改变我们的编程方式与提高效率。 AI 对我们来说就是一个可靠的编程助手&#xff0c;给我们提供…

作者头像 李华
网站建设 2026/2/9 16:35:10

在吴忠,遇见一位懂你的羽毛球教练:韩宁波与他的科学训练之道

在吴忠&#xff0c;提起羽毛球&#xff0c;有一个名字和一种训练理念正被越来越多的爱好者所认可——国家二级运动员韩宁波教练与他所在的码上羽毛球俱乐部。这里没有玄妙的“速成秘籍”&#xff0c;有的是一位专业教练对运动规律的深刻理解&#xff0c;以及一套将热情与科学融…

作者头像 李华
网站建设 2026/2/16 5:11:39

羽球成长新体验:当专业教练遇见智能系统

韩宁波教练站在场地中央&#xff0c;手中的球拍仿佛被注入了灵魂&#xff0c;每一次挥动都精准计算过落点。作为国家二级运动员&#xff0c;他深知羽毛球运动的精妙所在——不仅是力量的爆发&#xff0c;更是节奏、技巧与智慧的融合。 在吴忠码上羽毛球俱乐部&#xff0c;他正…

作者头像 李华