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

一个人想着一个人 提交于 2021-02-07 05:37:45

问题


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.

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

return empty ArrayList, if the return type is void:

 void batchInsert(List<User>)

return 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.

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?


回答1:


To get method return type/class from a ProceedingJoinPoint you can do this:

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


来源:https://stackoverflow.com/questions/28092800/when-using-spring-aoparound-how-can-i-get-return-type-of-the-pointcut-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!