java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring AOP通知类型

Spring AOP通知类型与实战示例讲解

作者:lzz的编码时刻

Spring AOP提供了五种通知类型:@Before、@After、@AfterReturning、@AfterThrowing和@Around,每种通知类型都有其特定的使用场景和实现方式,通过合理使用这些通知类型,可以实现各种横切关注点的模块化和解耦,感兴趣的朋友跟随小编一起看看吧

1. @Before 前置通知

1.1 基本说明

1.2 实现示例

@Aspect
@Component
public class SecurityAspect {
    @Before("@annotation(requiresAuth)")
    public void checkAuth(JoinPoint joinPoint, RequiresAuth requiresAuth) {
        // 获取当前用户信息
        ServletRequestAttributes attributes = 
            (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("Authorization");
        // 验证token
        if (!tokenService.isValid(token)) {
            throw new UnauthorizedException("无效的认证令牌");
        }
        // 检查权限
        String requiredRole = requiresAuth.role();
        if (!hasRole(token, requiredRole)) {
            throw new ForbiddenException("权限不足");
        }
    }
}

1.3 典型应用场景

1.4 获取参数

1.4.1 基本参数获取

@Before("execution(* com.example.service.*.*(..))")
public void beforeAdvice(JoinPoint joinPoint) {
    // 获取方法参数
    Object[] args = joinPoint.getArgs();
    // 获取方法签名
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    String methodName = signature.getName();
    // 获取参数名称
    String[] parameterNames = signature.getParameterNames();
    // 获取参数类型
    Class<?>[] parameterTypes = signature.getParameterTypes();
    // 打印参数信息
    for (int i = 0; i < args.length; i++) {
        logger.info("Parameter {} ({}) = {}", parameterNames[i], parameterTypes[i].getSimpleName(), args[i]);
    }
}

1.4.2 获取注解参数

@Before("@annotation(logParams)")
public void beforeWithAnnotation(JoinPoint joinPoint, LogParams logParams) {
    // 直接获取注解属性
    String description = logParams.description();
    boolean logResult = logParams.logResult();
    // 获取方法参数
    Object[] args = joinPoint.getArgs();
    // 根据注解配置记录日志
    if (logParams.includeParameters()) {
        Arrays.stream(args)
              .forEach(arg -> logger.info("Parameter value: {}", arg));
    }
}

2. @After 后置通知

2.1 基本说明

2.2 实现示例

@Aspect
@Component
public class ResourceCleanupAspect {
    @After("execution(* com.example.service.FileService.*(..))")
    public void cleanup(JoinPoint joinPoint) {
        try {
            // 清理临时文件
            String methodName = joinPoint.getSignature().getName();
            logger.info("Cleaning up resources after method: {}", methodName);
            cleanupTempFiles();
            // 释放其他资源
            releaseResources();
        } catch (Exception e) {
            logger.error("Cleanup failed", e);
        }
    }
    private void cleanupTempFiles() {
        // 清理临时文件的具体实现
    }
    private void releaseResources() {
        // 释放资源的具体实现
    }
}

2.3 典型应用场景

2.4 参数获取

@After("execution(* com.example.service.*.*(..)) && args(id,name,..)")
public void afterAdvice(JoinPoint joinPoint, Long id, String name) {
    // 直接使用参数
    logger.info("Method executed with ID: {} and name: {}", id, name);
    // 获取目标类信息
    Class<?> targetClass = joinPoint.getTarget().getClass();
    // 获取代理类信息
    Class<?> proxyClass = joinPoint.getThis().getClass();
}

3. @AfterReturning 返回通知

3.1 基本说明

3.2 实现示例

@Aspect
@Component
public class ResponseHandlerAspect {
    @AfterReturning(
        pointcut = "execution(* com.example.controller.*.*(..))",
        returning = "result"
    )
    public void handleResponse(JoinPoint joinPoint, Object result) {
        if (result instanceof List) {
            // 对集合类型结果进行脱敏处理
            List<?> list = (List<?>) result;
            for (Object item : list) {
                if (item instanceof UserDTO) {
                    UserDTO user = (UserDTO) item;
                    user.setPhone(maskPhoneNumber(user.getPhone()));
                    user.setEmail(maskEmail(user.getEmail()));
                }
            }
        }
    }
    private String maskPhoneNumber(String phone) {
        // 手机号码脱敏逻辑
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }
    private String maskEmail(String email) {
        // 邮箱脱敏逻辑
        return email.replaceAll("(\\w{3})\\w+(@\\w+\\.\\w+)", "$1***$2");
    }
}

3.3 典型应用场景

4. @AfterThrowing 异常通知

4.1 基本说明

4.2 实现示例

@Aspect
@Component
public class ExceptionHandlerAspect {
    @AfterThrowing(
        pointcut = "execution(* com.example.service.*.*(..))",
        throwing = "ex"
    )
    public void handleException(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        // 记录详细错误信息
        logger.error("Exception in {}.{}: {}", className, methodName, ex.getMessage());
        // 发送告警
        if (ex instanceof DataAccessException) {
            alertService.sendDatabaseAlert(className, methodName, ex);
        }
        // 异常分类统计
        metricService.incrementExceptionCounter(className, methodName, ex.getClass().getSimpleName());
        // 如果需要,可以转换异常类型
        if (ex instanceof SQLException) {
            throw new DatabaseException("数据库操作失败", ex);
        }
    }
}

4.3 典型应用场景

5. @Around 环绕通知

5.1 基本说明

5.2 实现示例

@Aspect
@Component
public class CacheAspect {
    @Autowired
    private CacheManager cacheManager;
    @Around("@annotation(cacheable)")
    public Object handleCache(ProceedingJoinPoint joinPoint, Cacheable cacheable) throws Throwable {
        // 构建缓存key
        String key = buildCacheKey(joinPoint, cacheable);
        // 尝试从缓存获取
        Object cachedValue = cacheManager.get(key);
        if (cachedValue != null) {
            logger.debug("Cache hit for key: {}", key);
            return cachedValue;
        }
        // 执行目标方法
        long startTime = System.currentTimeMillis();
        Object result = null;
        try {
            result = joinPoint.proceed();
            // 记录执行时间
            long executionTime = System.currentTimeMillis() - startTime;
            logger.debug("Method execution time: {}ms", executionTime);
            // 如果执行时间超过阈值,发送告警
            if (executionTime > 1000) {
                alertService.sendPerformanceAlert(joinPoint, executionTime);
            }
        } catch (Exception e) {
            // 异常处理
            logger.error("Method execution failed", e);
            throw e;
        }
        // 将结果放入缓存
        if (result != null) {
            cacheManager.put(key, result, cacheable.ttl());
        }
        return result;
    }
    private String buildCacheKey(ProceedingJoinPoint joinPoint, Cacheable cacheable) {
        // 缓存key构建逻辑
        StringBuilder key = new StringBuilder();
        key.append(joinPoint.getSignature().getDeclaringTypeName())
           .append(".")
           .append(joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            key.append(":");
            for (Object arg : args) {
                key.append(arg != null ? arg.toString() : "null").append(",");
            }
        }
        return key.toString();
    }
}

5.3 典型应用场景

@Aspect
@Component
public class RateLimiterAspect {
    private final RateLimiter rateLimiter = RateLimiter.create(100.0); // 每秒100个请求
    @Around("@annotation(rateLimited)")
    public Object limitRate(ProceedingJoinPoint joinPoint, RateLimited rateLimited) throws Throwable {
        if (!rateLimiter.tryAcquire(100, TimeUnit.MILLISECONDS)) {
            throw new TooManyRequestsException("请求过于频繁,请稍后重试");
        }
        return joinPoint.proceed();
    }
}

6. 最佳实践

到此这篇关于Spring AOP通知类型详解与实战的文章就介绍到这了,更多相关Spring AOP通知类型内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文