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

zhu*_*wei 6 java spring spring-aop

我现在有一个要求,就是当使用mybatis(特别是那些批处理执行sql)时,先检查参数,如果参数为null或为空,则只返回,不要继续,如果返回类型是List,例如.

List<User> getByIds(List<Long> idList)
Run Code Online (Sandbox Code Playgroud)

返回空ArrayList,如果返回类型为void:

 void batchInsert(List<User>)
Run Code Online (Sandbox Code Playgroud)

返回null.目的是避免这种情况,例如.

select * from user where id in ()
insert into user(name,email) values ()
Run Code Online (Sandbox Code Playgroud)

但是从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();
Run Code Online (Sandbox Code Playgroud)

那么如何在aop中得到返回类型:around?

dav*_*ooh 16

要从a获取方法返回类型/类,ProceedingJoinPoint您可以这样做:

Signature signature =  proceedingJoinPoint.getSignature();
Class returnType = ((MethodSignature) signature).getReturnType();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!有用!但我决定通过 mybatis 拦截器来应用它。 (2认同)