Spring aop切入点表达式访问方法返回类型

发布于 2024-09-06 20:33:08 字数 630 浏览 5 评论 0原文

我有一个包含许多方法的服务接口,所有这些方法都采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-09-13 20:33:08

JoinPoint提到了一个 getSignature() 方法,其返回类型 Signature 有一个子接口 MethodSignature 您可以尝试转换为,它有一个方法getReturnType(),这可能就是您正在寻找的。

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.

失退 2024-09-13 20:33:08

您可以在周围建议方法内进行检查(在您的情况下为handleServiceCall())

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文