Spring aop切入点表达式访问方法返回类型
我有一个包含许多方法的服务接口,所有这些方法都采用 Request 对象并返回 Response 对象。所有请求对象都有一个共同的祖先,所有响应对象都有一个不同的共同祖先(它有一个成功标志和一个消息字段)。
现在我想要一个围绕方面来检查权限等,执行服务调用并在出现任何失败时返回带有失败代码的响应对象。问题是:我需要知道要创建什么类型的 Response 对象。是否有一个切入点表达式可以让我访问返回类型?也许是这样的?
@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){ ... }
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){ ... }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JoinPoint
提到了一个
getSignature()
方法,其返回类型Signature
有一个子接口MethodSignature
您可以尝试转换为,它有一个方法getReturnType()
,这可能就是您正在寻找的。The Javadoc for
JoinPoint
mentions agetSignature()
method, whose return typeSignature
has a sub interfaceMethodSignature
you could try casting to, which has a methodgetReturnType()
, which might be what you are looking for.您可以在周围建议方法内进行检查(在您的情况下为handleServiceCall())
You can do that check inside the around advice method(handleServiceCall() in your case)