现在看具体实现。
一、AOP 到底对什么使用?
AOP主要针对横切关注点,这些代码通常:
- 分散在各处:相同的逻辑出现在多个地方
- 与业务逻辑无关:不是核心业务,但又必须存在
- 容易忘记或重复:如每个方法都要加日志
典型应用场景:
// 没有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;}}五、最佳实践建议
- 保持切面简单:每个切面只做一件事
- 使用自定义注解:精确控制哪些方法需要增强
- 注意执行顺序:特别是安全、事务等有依赖关系的切面
- 性能考虑:切入点表达式要精确,避免匹配过多方法
- 异常处理:环绕通知要正确传递或转换异常
六、调试技巧
// 查看AOP是否生效@Aspect@ComponentpublicclassDebugAspect{@Before("execution(* com.example..*.*(..))")publicvoiddebug(JoinPointjoinPoint){System.out.println("AOP拦截: "+joinPoint.getTarget().getClass().getSimpleName()+"."+joinPoint.getSignature().getName());}}AOP的核心价值在于:让关注点分离,让业务代码保持干净。当你发现某个非业务逻辑(日志、权限、缓存等)在多个地方重复出现时,就应该考虑使用AOP来解决了。