本文主要是介绍SpringBoot中四种AOP实战应用场景及代码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO...
引言
面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护。在SpringBoot应用中,AOP能够帮助我们优雅地解决横切关注点问题,如日志记录、权限控制、性能监控等,这些功能往往贯穿整个应用但又不属于业务核心逻辑。
本文将介绍SpringBoot中4种AOP实战应用场景,包括代码实现、核心原理及实践。
场景一:日志记录与性能监控
业务需求
在企业级应用中,我们通常需要:
- 记录API请求的调用情况
- 监控方法执行时间,发现性能瓶颈
- 追踪方法调用的入参和返回结果
实现方案
@ASPect @Component @Slf4j public class LoggingAspect { /** * 定义切点:所有controller包下的所有方法 */ @Pointcut("execution(* com.example.demo.controller.*.*(..))") public void controllerMethods() {} /** * 环绕通知:记录请求日志和执行时间 */ @Around("controllerMethods()") public Object logAroundControllers(ProceedingJoinPoint joinPoint) throws Throwable { // 获取方法签名 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getName(); String className = signature.getDeclaringTypeName(); // 记录请求参数 String params = Arrays.toString(joinPoint.getArgs()); log.info("Request to {}.{} with params: {}", className, methodName, params); // 记录开始时间 long startTime = System.currentTimeMillis(); // 执行目标方法 Object result; try { result = joinPoint.proceed(); // 计算执行时间 long executionTime = System.currentTimeMillis() - startTime; // 记录返回结果和执行时间 log.info("Response from {}.{} ({}ms): {}", className, methodName, executionTime, result); // 记录慢方法 if (executionTime > 1000) { log.warn("Slow execution detected! {}.{} took {}ms", className, methodName, executionTime); } return result; } catch (Exception e) { // 记录异常信息 log.error("Exception in {}.{}: {}", className, methodName, e.getMessage(), e); throw e; } } /** * 定义服务层方法切点 */ @Pointcut("execution(* com.example.demo.service.*.*(..))") public void serviceMethods() {} /** * 记录服务层方法的关键调用 */ @Before("serviceMethods() && @annotation(logMethod)") public void logServiceMethod(JoinPoint joinPoint, LogMethod logMethod) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getName(); // 获取参数名 String[] paramNames = signature.getParameterNames(); Object[] args = joinPoint.getArgs(); StringBuilder logMessage = new StringBuilder(); logMessage.append("Executing ").append(methodName).append(" with params: {"); for (int i = 0; i < paramNames.length; i++) { logMessage.append(paramNames[i]).append("=").append(args[i]); if (i < paramNames.length - 1) { logMessage.append(", "); } } ToNEglg logMessage.append("}"); // 根据注解设置的级别记录日志 switch (logMethod.level()) { case DEBUG: log.debug(logMessage.toString()); break; case INFO: log.info(logMessage.toString()); break; case WARN: log.warn(logMessage.toString()); break; case ERROR: log.error(logMessage.toString()); break; } } } /** * 自定义日志注解 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LogMethod { LogLevel level() default LogLevel.INFO; public enum LogLevel { DEBUG, INFO, WARN, ERROR } }
使用示例
@Service public class UserService { @LogMethod(level = LogMethod.LogLevel.INFO) public User findById(Long id) { // 业务逻辑 return userRepository.findById(id).orElse(null); } @LogMethod(level = LogMethod.LogLevel.WARN) public void updateUserStatus(Long userId, String status) { // 更新用户状态 } }
扩展:MDC实现请求跟踪
@Component public class RequestIdFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { // 为每个请求生成唯一ID String requestId = UUID.randomUUID().toString().replace("-", ""); MDC.put("requestId", requestId); if (request instanceof HttpServletRequest) { HttpServletRequest httpRequest = (HttpServletRequest) request; // 记录用户信息 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth != null && auth.isAuthenticated()) { MDC.put("userId", auth.getName()); } MDC.put("remoteAddr", httpRequest.getRemoteAddr()); } chain.doFilter(request, response); } finally { // 请求完成后清理MDC MDC.clear(); } } }
场景二:权限控制与安全增强
业务需求
在企业应用中,权限控制是一个常见的需求:
- 基于角色的接口访问控制
- 细粒度的操作权限控制
- 对敏感数据访问的记录
实现方案
首先,创建自定义注解:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface RequiresPermission { /** * 所需权限编码数组,满足其中任一即可 */ String[] value() default {}; /** * 权限逻辑类型:AND(同时具有所有权限), OR(满足任一权限即可) */ LogicalType logical() default LogicalType.OR; public enum LogicalType { AND, OR } }
实现权限切面:
@Aspect @Component @Slf4j public class PermissionAspect { @Autowired private UserService userService; /** * 定义切点:所有带有@RequiresPermission注解的方法 */ @Pointcut("@annotation(com.example.demo.annotation.RequiresPermission)") public void permissionCheck() {} /** * 权限验证前置通知 */ @Before("permissionCheck() && @annotation(requiresPermission)") public void checkPermission(JoinPoint joinPoint, RequiresPermission requiresPermission) { // 获取当前用户 User currentUser = getCurrentUser(); if (currentUser == null) { throw new UnauthorizedException("用户未登录或会话已过期"); } // 获取用户权限列表 Set<String> userPermissions = userService.getUserPermissions(currentUser.getId()); // 获取注解中要求的权限 String[] requiredPermissions = requiresPermission.value(); RequiresPermission.LogicalType logicalType = requiresPermission.logical(); // 权限校验 booleaToNEglgn hasPermission = false; if (logicalType == RequiresPermission.LogicalType.OR) { // 满足任一权限即可 for (String permission : requiredPermissions) { if (userPermissions.contains(permission)) { hasPermission = true; break; } } } else { // 必须同时满足所有权限 hasPermission = true; for (String permission : requiredPermissions) { if (!userPermissions.contains(permission)) { hasPermission = false; break; } } } if (!hasPermission) { log.warn("用户 {} 尝试访问未授权资源: {}.{}", currentUser.getUsername(), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); throw new ForbiddenException("权限不足,无法执行该操作"); } // 记录敏感操作 log.info("用户 {} 执行了需授权操作: {}.{}", currentUser.getUsername(), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); } /** * 定义切点:带有@RequiresRole注解的方法 */ @Pointcut("@annotation(com.example.demo.annotation.RequiresRole)") public void roleCheck() {} /** * 角色检查前置通知 */ @Before("roleCheck() && @annotation(requiresRole)") public void checkRole(JoinPoint joinPoint, RequiresRole requiresRole) { // 获取当前用户 User currentUser = getCurrentUser(); if (currentUser == null) { throw new UnauthorizedException("用户未登录或会话已过期"); } // 获取用户角色 Set<String> userRoles = userService.getUserRoles(currentUser.getId()); // 获取注解中要求的角色 String[] requiredRoles = requiresRole.value(); // 角色校验 boolean hasRole = false; for (String role : requiredRoles) { if (userRoles.contains(role)) { hasRole = true; break; } } if (!hasRole) { log.warn("用户 {} 尝试访问未授权角色资源: {}.{}", currentUser.getUsername(), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); throw new ForbiddenException("角色不足,无法执行该操作"); } } /** * 数据权限过滤切点:针对查询方法 */ @Pointcut("execution(* com.example.demo.service.*.find*(..))") public void dataPermissionFilter() {} /** * 数据权限过滤通知 */ @Around("dataPermissionFilter()") public Object filterDataByPermission(ProceedingJoinPoint joinPoint) throws Throwable { // 获取当前用户 User currentUser = getCurrentUser(); // 默认情况下执行原方法 Object result = joinPoint.proceed(); // 如果是管理员,无需过滤数据 if (userService.isAdmin(currentUser.getId())) { return result; } // 对查询结果进行过滤 if (result instanceof Collection) { Collection<?> collection = (Collection<?>) result; // 实现数据过滤逻辑... } else if (result instanceof Page) { Page<?> page = (Page<?>) result; // 实现分页数据过滤... } return result; } /** * 获取当前登录用户 */ private User getCurrentUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || !authentication.isAuthenticated()) { return null; } Object principal = authentication.getPrincipal(); if (principal instanceof User) { return (User) principal; } return null; } } /** * 自定义角色注解 */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface RequiresRole { String[] value(); }
使用示例
@RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; @GetMapping @RequiresPermission("user:list") public List<User> listUsers() { return userService.findAll(); } @GetMapping("/{id}") @RequiresPermission("user:view") public User getUser(@PathVariable Long id) { return userService.findById(id); } @PostMapping @RequiresPermission(value = {"user:create", "user:edit"}, logical = RequiresPermission.LogicalType.OR) public User createUser(@RequestBody User user) { return userService.save(user); } @DeleteMapping("/{id}") @RequiresRole("ADMIN") public void deleteUser(@PathVariable Long id) { userService.delete(id); } @PutMapping("/{id}/status") @RequiresPermission(value = {"user:edit", "user:manage"}, logical = RequiresPermission.LogicalType.AND) public User updateUserStatus(@PathVariable Long id, @RequestParam String status) { return userService.updateStatus(id, status); } }
场景三:自定义缓存实现
业务需求
缓存是提升应用性能的关键手段,通过AOP可以实现:
- 自定义缓存策略,满足特定业务需求
- 细粒度的缓存控制
- 灵活的缓存键生成和过期策略
实现方案
首先定义缓存注解:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Cacheable { /** * 缓存名称 */ String cacheName(); /** * 缓存键表达式,支持SpEL表达式 */ String key() default ""; /** * 过期时间(秒) */ long expireTime() default 300; /** * 是否使用方法参数作为缓存键的一部分 */ boolean useMethodParameters() default true; } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CacheEvict { /** * 缓存名称 */ String cacheName(); /** * 缓存键表达式 */ String key() default ""; /** * 是否清除所有缓存 */ boolean allEntries() default false; }
实现缓存切面:
@Aspect @Component @Slf4j public class CacheAspect { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private CacheKeyGenerator keyGenerator; /** * 定义缓存获取切点 */ @Pointcut("@annotation(com.example.demo.annotation.Cacheable)") public void cacheableOperation() {} /** * 定义缓存清除切点 */ @Pointcut("@annotation(com.example.demo.annotation.CacheEvict)") public void cacheEvictOperation() {} /** * 缓存环绕通知 */ @Around("cacheableOperation() && @annotation(cacheable)") public Object handleCacheable(ProceedingJoinPoint joinPoint, Cacheable cacheable) throws Throwable { // 生成缓存键 String cacheKey = generateCacheKey(joinPoint, cacheable.cacheName(), cacheable.key(), cacheable.useMethodParameters()); // 检查缓存中是否已有数据 Boolean hasKey = redisTemplate.hasKey(cacheKey); if (Boolean.TRUE.equals(hasKey)) { Object cachedValue = redisTemplate.opsForValue().get(cacheKey); log.debug("Cache hit for key: {}", cacheKey); return cachedValue; } // 缓存未命中,执行方法获取结果 log.debug("Cache miss for key: {}", cacheKey); Object result = joinPoint.proceed(); // 将结果存入缓存 if (result != null) { redisTemplate.opsForValue().set(cacheKey, result, cacheable.expireTime(), TimeUnit.SECONDS); log.debug("Stored in cache with key: {}, expire time: {}s", cacheKey, cacheable.expireTime()); } return result; } /** * 缓存清除前置通知 */ @Before("cacheEvictOperation() && @annotation(cacheEvict)") public void handleCacheEvict(JoinPoint joinPoint, CacheEvict cacheEvict) { if (cacheEvict.allEntries()) { // 清除该缓存名称下的所有条目 String cachePattern = cacheEvict.cacheName() + ":*"; Set<String> keys = redisTemplate.keys(cachePattern); if (keys != null && !keys.isEmpty()) { redisTemplate.delete(keys); log.debug("Cleared all cache entries with pattern: {}", cachePattern); } } else { // 清除指定键的缓存 String cacheKey = generateCacheKey(joinPoint, cacheEvict.cacheName(), cacheEvict.key(), true); redisTemplate.delete(cacheKey); log.debug("Cleared cache with key: {}", cacheKey); } } /** * 生成缓存键 */ private String generateCacheKey(JoinPoint joinPoint, String cacheName, String keyExpression, boolean useParams) { StringBuilder keyBuilder = new StringBuilder(cacheName).append(":"); // 如果提供了自定义键表达式 if (StringUtils.hasText(keyExpression)) { String evaLuatedKey = keyGenerator.generateKey(keyExpression, joinPoint); keyBuilder.append(evaluatedKey); } else if (useParams) { // 使用方法签名和参数作为键 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getName(); keyBuilder.append(methodName); // 添加参数 Object[] args = joinPoint.getArgs(); if (args != null && args.length > 0) { for (Object arg : args) { if (arg != null) { keyBuilder.append(":").append(arg.hashCode()); } else { keyBuilder.append(":null"); } } } } else { // 仅使用方法名 keyBuilder.append(joinPoint.getSignature().getName()); } return keyBuilder.toString(); } } /** * 缓存键生成器,支持SpEL表达式 */ @Component public class CacheKeyGenerator { private final ExpressionParser parser = new SpelExpressionParser(); private final StandardEvaluationContext context = new StandardEvaluationContext(); public String generateKey(String expression, JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Object[] args = joinPoint.getArgs(); String[] parameterNames = signature.getParameterNames(); // 设置方法参数为上下文变量 for (int i = 0; i < parameterNames.length; i++) { context.setVariable(parameterNames[i], args[i]); } // 添加额外的元数据 context.setVariable("method", method.getName()); context.setVariable("class", method.getDeclaringClass().getSimpleName()); context.setVariable("target", joinPoint.getTarget()); // 执行表达式 Expression exp = parser.parseExpression(expression); return exp.getValue(context, String.class); } }
Redis配置
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); // 使用Jackson2jsonRedisSerializer序列化值 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 设置键的序列化方式为字符串 template.setKeySerializer(new StringRedisSerializer()); // 值使用JSON序列化 template.setValueSerializer(jackson2JsonRedisSerializer); // Hash键也使用字符串 template.setHashKeySerializer(new StringRedisSerializer()); // Hash值使用JSON序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
使用示例
@Service public class ProductService { @Autowired private ProductRepository productRepository; @Cacheable(cacheName = "products", expireTime = 3600) public Product getById(Long id) { return productRepository.findById(id).orElse(null); } @Cacheable(cacheName = "products", key = "'list:category:' + #categoryId", expireTime = 1800) public List<Product> getByCategory(Long categoryId) { return productRepository.findByCategoryId(categoryId); } @CacheEvict(cacheName = "products", allEntries = true) public Product save(Product product) { return productRepository.save(product); } @CacheEvict(cacheName = "products", key = "'list:category:' + #product.categoryId") public void deleteProductFromCategory(Product product) { productRepository.delete(product); } }
场景四:统一异常处理与重试机制
业务需求
在分布式系统或复杂业务场景中,我们常常需要:
- 优雅地处理异常
- 对某些操作进行自动重试
- 对关键操作进行幂等性保证
实现方案
首先定义重试和异常处理注解:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Retryable { /** * 最大重试次数 */ int maxAttempts() default 3; /** * 重试间隔(毫秒) */ long backoff() default 1000; /** * 指定捕获的异常类型 */ Class<? extends Throwable>[] value() default {Exception.class}; /** * 重试策略 */ RetryStrategy strategy() default RetryStrategy.FIXED; /** * 重试策略枚举 */ enum RetryStrategy { /** * 固定间隔 */ FIXED, /** * 指数退避 */ EXPONENTIAL } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Idempotent { /** * 幂等键表达式 */ String key(); /** * 过期时间(秒) */ long expireSeconds() default 300; }
实现异常处理和重试切面:
@Aspect @Component @Slf4j public class RetryAspect { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 定义可重试操作切点 */ @Pointcut("@annotation(com.example.demo.annotation.Retryable)") public void retryableOperation() {} /** * 定义幂等操作切点 */ @Pointcut("@annotation(com.example.demo.annotation.Idempotent)") public void idempotentOperation() {} /** * 重试环绕通知 */ @Around("retryableOperation() && @annotation(retryable)") public Object handleRetry(ProceedingJoinPoint joinPoint, Retryable retryable) throws Throwable { int attempts = 0; Class<? extends Throwable>[] retryableExceptions = retryable.value(); Retryable.RetryStrategy strategy = retryable.strategy(); while (true) { attempts++; try { // 执行目标方法 return joinPoint.proceed(); } catch (Throwable t) { // 检查是否是需要重试的异常类型 boolean shouldRetry = false; for (Class<? extends Throwable> exceptionType : retryableExceptions) { if (exceptionType.isInstance(t)) { shouldRetry = true; break; } } // 如果不需要重试,或者达到最大重试次数,则抛出异常 if (!shouldRetry || attempts >= retryable.maxAttempts()) { log.warn("Method {} failed after {} attempts: {}", joinPoint.getSignature().getName(), attempts, t.getMessage()); throw t; } // 计算重试等待时间 long waitTime; if (strategy == Retryable.RetryStrategy.EXPONENTIAL) { // 指数退避: 基础时间 * 2^(尝试次数-1) waitTime = retryable.backoff() * (long) Math.pow(2, attempts - 1); } else { // 固定间隔 waitTime = retryable.backoff(); } log.info("Retrying {} (attempt {}/{}) after {} ms due to: {}", joinPoint.getSignature().getName(), attempts, retryable.maxAttempts(), waitTime, t.getMessage()); // 等待指定时间后重试 Thread.sleep(waitTime); } } } /** * 幂等性环绕通知 */ @Around("idempotentOperation() && @annotation(idempotent)") public Object handleIdempotent(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable { // 解析幂等键 String idempotentKey = resolveIdempotentKey(joinPoint, idempotent.key()); String lockKey = "idempotent:" + idempotentKey; // 尝试设置分布式锁 Boolean success = redisTemplate.opsForValue().setIfAbsent( lockKey, "PROCESSING", idempotent.expireSeconds(), TimeUnit.SECONDS); if (Boolean.TRUE.equals(success)) { try { // 获取锁成功,执行业务逻辑 Object result = joinPoint.proceed(); // 将结果存入Redis String resultKey = "result:" + lockKey; redisTemplate.opsForValue().set( resultKey, new ObjectMapper().writeValueAsString(result), idempotent.expireSeconds(), TimeUnit.SECONDS); // 标记为已处理 redisTemplate.opsForValue().set( lockKey, "COMPLETED", idempotent.expireSeconds(), TimeUnit.SECONDS); return result; } catch (Throwable t) { // 处理失败,标记错误 redisTemplate.opsForValue().set( lockKey, "ERROR:" + t.getMessage(), idempotent.expireSeconds(), TimeUnit.SECONDS); throw t; } } else { // 获取锁失败,表示操作正在处理或已处理 String status = redisTemplate.opsForValue().get(lockKey); if ("PROCESSING".equals(status)) { // 还在处理中 throw new ConcurrentOperationException("操作正在处理中,请勿重复提交"); } else if (status != null && status.startsWith("ERROR:")) { // 之前处理出错 throw new OperationFailedException("操作处理失败: " + status.substring(6)); } else if ("COMPLETED".equals(status)) { // 已完成,尝试返回之前的结果 String resultKey = "result:" + lockKey; String resultJson = redisTemplate.opsForValue().get(resultKey); if (resultJson != null) { // 将JSON反序列化为响应对象 Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); Class<?> returnType = method.getReturnType(); try { return new ObjectMapper().readValue(resultJson, returnType); } catch (Exception e) { log.error("Failed to deserialize cached result: {}", e.getMessage()); } } // 如果没有找到结果或反序列化失败,返回成功但无法提供上次结果的消息 throw new OperationAlreadyCompletedException("操作已成功处理,但无法提供上次操作的结果"); } // 状态未知,抛出异常 throw new OperationFailedException("操作状态未知"); } } /** * 解析幂等键表达式 */ private String resolveIdempotentKey(JoinPoint joinPoint, String keyExpression) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String[] paramNames = signature.getParameterNames(); Object[] args = joinPoint.getArgs(); // 创建表达式上下文 StandardEvaluationContext context = new StandardEvaluationContext(); // 添加方法参数 for (int i = 0; i < paramNames.length; i++) { context.setVariable(paramNames[i], args[i]); } // 添加类名和方法名 context.setVariable("method", signature.getMethod().getName()); context.setVariable("class", signature.getDeclaringType().getSimpleName()); // 解析表达式 ExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression(keyExpression); return expression.getValue(context, String.class); } } // 自定义异常类 public class ConcurrentOperationException extends RuntimeException { public ConcurrentOperationException(String message) { super(message); } } public class OperationFailedException extends RuntimeException { public OperationFailedException(String message) { super(message); } } public class OperationAlreadyCompletedException extends RuntimeException { public OperationAlreadyCompletedException(String message) { super(message); } }
使用示例
@Service public class PaymentService { @Autowired private PaymentGateway paymentGateway; @Autowired private OrderRepository orderRepository; /** * 远程支付处理,可能遇到网络问题需要重试 */ @Retryable( value = {ConnectException.class, TimeoutException.class, PaymentGatewayException.class}, maxAttempts = 3, backojsff = 2000, strategy = Retryabphple.RetryStrategy.EXPONENTIAL ) public PaymentResult processPayment(String orderId, BigDecimal amount) { log.info("Processing payment for order {} with amount {}", orderId, amount); // 调用远程支付网关 return paymentGateway.processPayment(orderId, amount); } /** * 订单退款,需要保证幂等性 */ @Idempotent(key = "'refund:' + #orderId", expireSeconds = 3600) public RefundResult refundOrder(String orderId) { Order order = orderRepository.findById(orderId) .orElseThrow(() -> new OrderNotFoundException("Order not found: " + orderId)); // 验证订单状态 if (!"PAID".equals(order.getStatus())) { throw new InvalidOrderStatusException("Cannot refund order with status: " + order.getStatus()); } // 调用支付网关退款 RefundResult result = paymentGateway.refund(order.getPaymentId(), order.getTotalAmount()); // 更新订单状态 order.setStatus("REFUNDED"); order.setRefundTime(LocalDateTime.now()); orderRepository.save(order); return result; android } } @Service public class StockService { @Autowired private StockRepository stockRepository; /** * 扣减库存,需要在分布式环境下重试和幂等 */ @Retryable( value = {OptimisticLockException.class, StockInsufficientException.class}, maxAttempts = 5, backoff = 500 ) @Idempotent(key = "'deduct:' + #orderId") public void deductStock(String orderId, List<OrderItem> items) { // 检查是否存在库存记录 for (OrderItem item : items) { Stock stock = stockRepository.findByProductId(item.getProductId()); if (stock == null) { throw new ProductNotFoundException("Product not found: " + item.getProductId()); } if (stock.getAvailable() < item.getQuantity()) { throw new StockInsufficientException( "Insufficient stock for product: " + item.getProductId() + ", requested: " + item.getQuantity() + ", available: " + stock.getAvailable()); } } // 执行库存扣减 for (OrderItem item : items) { stockRepository.deductStock(item.getProductId(), item.getQuantity()); } } }
结论
AOP是SpringBoot中一个强大的编程范式,通过这些模式,我们可以将横切关注点与业务逻辑解耦,使代码更加模块化、可维护,同时提高系统的健壮性和安全性。
以上就是SpringBoot中四种AOP实战应用场景及代码实现的详细内容,更多关于SpringBoot AOP应用场景的资料请关注China编程(www.chinasem.cn)其它相关文章!
这篇关于SpringBoot中四种AOP实战应用场景及代码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!