欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 【Spring】AOP在实际项目中的运用

【Spring】AOP在实际项目中的运用

2025/3/13 9:15:56 来源:https://blog.csdn.net/weixin_45325628/article/details/146186999  浏览:    关键词:【Spring】AOP在实际项目中的运用

一、AOP 的核心思想:注解驱动

通过自定义注解标记需要增强的方法,切面逻辑根据注解动态增强代码。业务代码无需感知具体逻辑,只需关注自身职责


二、电商通用场景示例

1. 接口防重提交(幂等性控制)

定义注解:标记需要幂等控制的接口。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Idempotent {String key() default "";  // 支持动态生成幂等键int expire() default 5;    // 默认锁定时长5秒
}

切面逻辑:统一处理幂等键的生成与校验。

@Aspect
public class IdempotentAspect {@Around("@annotation(idempotent)")public Object checkIdempotent(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable {String key = generateKey(joinPoint, idempotent); // 动态生成幂等键if (redis.setIfAbsent(key, "1", idempotent.expire(), TimeUnit.SECONDS)) {return joinPoint.proceed(); // 执行业务逻辑} else {throw new BusinessException("请勿重复请求!");}}
}

业务代码:只需添加注解,无需编写防重逻辑。

@Idempotent(key = "#order.userId + '-' + #order.productId") // 动态生成键
public void createOrder(Order order) {// 业务逻辑(无需关心防重)
}

2. 接口权限校验(动态鉴权)

定义注解:标记需要鉴权的接口。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequirePermission {String value();  // 权限标识(如 "order:create")
}

切面逻辑:统一从上下文获取用户权限并校验。

@Aspect
public class PermissionAspect {@Before("@annotation(RequirePermission)")public void checkPermission(JoinPoint joinPoint) {RequirePermission annotation = getAnnotation(joinPoint);if (!currentUser.hasPermission(annotation.value())) {throw new BusinessException("无权限操作!");}}
}

业务代码:仅用注解声明所需权限。

@RequirePermission("order:create")
public void createOrder(Order order) {// 业务逻辑(无需关心鉴权)
}

3. 热点数据缓存(自动缓存)

定义注解:标记需要缓存的方法。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CacheResult {String key();         // 缓存键(支持SpEL表达式)int expire() default 3600; // 缓存时间(秒)
}

切面逻辑:统一处理缓存的读取与写入。

@Aspect
public class CacheAspect {@Around("@annotation(cacheResult)")public Object cacheResult(ProceedingJoinPoint joinPoint, CacheResult cacheResult) throws Throwable {String key = parseKey(joinPoint, cacheResult); // 解析SpEL表达式生成键Object value = redis.get(key);if (value != null) return value;value = joinPoint.proceed(); // 执行业务逻辑redis.set(key, value, cacheResult.expire(), TimeUnit.SECONDS);return value;}
}

业务代码:只需关注数据查询逻辑。

@CacheResult(key = "'product:' + #productId") // 自动缓存结果
public Product getProductDetail(String productId) {return productDao.findById(productId);
}

4. 接口限流(动态阈值)

定义注解:标记需要限流的接口。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RateLimit {int permitsPerSecond() default 100; // 每秒允许请求数
}

切面逻辑:统一接入限流算法(如令牌桶)。

@Aspect
public class RateLimitAspect {private final Map<String, RateLimiter> limiters = new ConcurrentHashMap<>();@Around("@annotation(rateLimit)")public Object limit(ProceedingJoinPoint joinPoint, RateLimit rateLimit) throws Throwable {String key = generateKey(joinPoint); // 按方法名生成限流键RateLimiter limiter = limiters.computeIfAbsent(key, k -> RateLimiter.create(rateLimit.permitsPerSecond()));if (limiter.tryAcquire()) {return joinPoint.proceed();} else {throw new BusinessException("请求过于频繁!");}}
}

业务代码:仅用注解声明限流阈值。

@RateLimit(permitsPerSecond = 50) // 每秒最多50次请求
public void submitOrder(Order order) {// 业务逻辑(无需关心限流)
}

三、AOP 的核心优势

  1. 解耦性:业务代码不耦合横切逻辑(如防重、鉴权)。
  2. 复用性:同一套切面逻辑可被多个注解复用。
  3. 动态性:通过 SpEL 表达式实现注解参数的动态计算。
  4. 可维护性:修改横切逻辑时只需调整切面,无需改动业务代码。

四、对比直接在业务代码中实现

场景AOP + 注解业务代码硬编码
防重提交通过 @Idempotent 注解自动处理每个方法中编写 Redis 操作与异常处理
权限校验通过 @RequirePermission 注解自动拦截在每个方法开头调用权限服务
缓存逻辑通过 @CacheResult 注解自动缓存手动查询缓存、处理未命中与回填逻辑
限流控制通过 @RateLimit 注解自动限流在每个方法中集成限流算法与异常处理

五、总结

通过 自定义注解 + 动态切面,AOP 在电商项目中能实现:
快速接入通用能力:新接口只需添加注解即可获得防重、鉴权等功能。
统一逻辑管理:所有缓存、限流逻辑集中在切面中,避免代码分散。
灵活调整策略:修改限流阈值或缓存时间只需调整注解参数,无需修改业务代码。

这种设计模式特别适合中大型项目,能显著提升开发效率与系统可维护性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词