Spring aop pointcut expression to access method return type

☆樱花仙子☆ 提交于 2019-12-04 11:03:01

问题


I have a service interface with many methods, all of which take a Request object and return a Response object. All request objects have a common ancestor and all response objects have a different common ancestor (which has a success flag and a message field).

Now I want to have an around aspect that checks permissions etc, performs the service call and returns a Response object with a failure code if anything fails. The problem is: I need to know what type of Response object to create. Is there a pointcut expression that gives me access to the return type? Something like this, perhaps?

@Around(value = "execution(public *"
    + " com.mycompany.MyService+.*(..))"
    + " && args(request)"
    + " && returning( returnType)" // something like this would be nice

, argNames = "request,returnType")
public Object handleServiceCall(final ProceedingJoinPoint pjp,
    final Request request,
    final Class<? extends Response> returnType){ ... }

回答1:


The Javadoc for JoinPoint mentions a getSignature() method, whose return type Signature has a sub interface MethodSignature you could try casting to, which has a method getReturnType(), which might be what you are looking for.




回答2:


You can do that check inside the around advice method(handleServiceCall() in your case)

Object actuals = pjp.proceed();
if(actuals instanceof MyResponse){
//TO:DO: Your code here
}


来源:https://stackoverflow.com/questions/3100228/spring-aop-pointcut-expression-to-access-method-return-type

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