简体   繁体   English

当使用spring aop:around时,如何获得切入点方法的返回类型?

[英]When using spring aop:around, how can I get return type of the pointcut method?

I have a requirement now, that is when using mybatis(especially those batch execute sql), check parameter first , if the parameter is null or empty , then just return, don't proceed and if the return type is List,eg. 我现在有一个要求,就是当使用mybatis(特别是那些批处理执行sql)时,先检查参数,如果参数为null或为空,则只返回,不要继续,如果返回类型是List,例如。

List<User> getByIds(List<Long> idList)

return empty ArrayList, if the return type is void: 返回空ArrayList,如果返回类型为void:

 void batchInsert(List<User>)

return null. 返回null。 The purpose is to avoid this situation, eg. 目的是避免这种情况,例如。

select * from user where id in ()
insert into user(name,email) values ()

but from joinPoint I can't get return type,only can get args. 但是从joinPoint我无法获得返回类型,只能得到args。

Object[] args = joinPoint.getArgs();
if(args!=null&&args.length=1){
    if(args[0] instanceof List){
        if(((List) obj).isEmpty()){
                if(returnType.equals("java.util.List"))
                    return new ArrayList();
                else if(returnType.equals("void"))
                    return null;    
    }
}
return joinPoint.proceed();

So how can I get return type in aop:around? 那么如何在aop中得到返回类型:around?

To get method return type/class from a ProceedingJoinPoint you can do this: 要从ProceedingJoinPoint获取方法返回类型/类,您可以执行以下操作:

Signature signature =  proceedingJoinPoint.getSignature();
Class returnType = ((MethodSignature) signature).getReturnType();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM