Spring aop切入点表达式来访问方法返回类型 [英] Spring aop pointcut expression to access method return type

查看:141
本文介绍了Spring aop切入点表达式来访问方法返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有许多方法的服务接口,所有这些方法都接受一个Request对象并返回一个Response对象。所有请求对象都有一个共同的祖先,所有响应对象都有一个不同的共同祖先(它有一个成功标志和一个消息字段)。



现在我想要一个检查权限等的方面,执行服务调用并返回带有失败代码的Response对象(如果有任何失败)。问题是:我需要知道要创建什么类型的Response对象。是否有切入点表达式,使我可以访问返回类型?也许是这样的事情?

  @Around(value =执行(公共*
+com.mycompany .MyService +。*(..))
+&& args(request)
+&& returns(returnType)//这样的东西会很好

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


解决方案

JoinPoint 提到 getSignature()方法,其返回类型签名有一个子接口 MethodSignature 你可以尝试施放,有方法 getReturnType(),这可能就是你要找的。

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){ ... }

解决方案

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.

这篇关于Spring aop切入点表达式来访问方法返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆