spring aop的用法

本文介绍两种Spring AOP的实现方式,一是使用切入点方法,二是利用注解进行参数校验,详细解析了如何在代码中应用这些技术来增强功能。
部署运行你感兴趣的模型镜像

第一种:使用切入点

@Slf4j
@Component
@Aspect
public class DemoAop {

    @Autowired
    private ElasticSearchService<DemoEntity> esService;

    @Pointcut("execution(* com.xxx.xxx.mapper.xxmapper.DemoMapper.batchDemo(..))")
    public void pointCut() {
        //
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        log.info("es保存设备当前位置信息:{}", args);
        try {
            List<Demo> entityList = Lists.newArrayList();
            // 索引为0的位置保存所有参数信息
            if (args[0].getClass() == ArrayList.class) {
                List<Demo> demoList= (ArrayList)args[0];
                for (Demo  demo : demoList) {
                    entityList.add(demo);
                }
            }
            esService.save(entityList);
        } catch (Exception e) {
            log.error("设备当前位置信息保存es失败:{}", e);
        } finally {
            return pjp.proceed(args);
        }
    }

}

第二种:使用注解

@Component
@Aspect
public class ParameterCheckAopHandler {
    // 对有ParameterCheck注解的方法进行拦截
    @Around("@annotation(com.xxx.xx.xx.xx.ParameterCheck)")
    @Order(2)
    public Object parameterCheck(ProceedingJoinPoint joinPoint) throws Throwable {
        Object target = joinPoint.getTarget();
        Class<?> clazz = target.getClass();// 目标类
        String methodName = joinPoint.getSignature().getName();// 方法名
        Object[] args = joinPoint.getArgs();// 参数
        // 通过反射获取对象注解的方法
        Method method = ((MethodSignature)joinPoint.getSignature()).getMethod();
        // 获取该方法在参数上的注解,每个参数可以有多个注解,得到的是一个二维数组
        Annotation[][] parameterAnnotaions = method.getParameterAnnotations();
        if (parameterAnnotaions == null || parameterAnnotaions.length == 0) {
            return joinPoint.proceed(args);
        }
        for (int i = 0; i < parameterAnnotaions.length; i++) {
            Annotation[] oneParameterAnnotaions = parameterAnnotaions[i];
            if (oneParameterAnnotaions == null || oneParameterAnnotaions.length == 0) {
                continue;
            }
            for (int j = 0; j < oneParameterAnnotaions.length; j++) {
                // 1.正则校验
                if (oneParameterAnnotaions[j].annotationType() == Pattern.class) {
                    Pattern pattern = (Pattern)oneParameterAnnotaions[j];
                    if (args[i] != null) {
                        String regexp = pattern.regexp();
                        java.util.regex.Pattern patt = java.util.regex.Pattern.compile(regexp);
                        boolean matched = patt.matcher(args[i].toString()).matches();
                        if (!matched) {
                            throw new Exception(args[i] + "格式不正确");
                        }
                    }
                }
                // 2.针对user注解校验
                if (oneParameterAnnotaions[j].annotationType() == InjectUserInfo.class) {
                    UserInfo userInfo = (UserInfo)args[i];
                    if (null == userInfo || null == userInfo.getUser()) {
                        throw new Exception("无效用户信息");
                    }
                }
            }
        }
        return joinPoint.proceed(args);
    }
}

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值