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

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

问题描述

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

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

@Around(value = "execution(public *"+ " com.mycompany.MyService+.*(..))"+ " && args(请求)"+ " &&returning(returnType)"//像这样的东西会很好, argNames = "请求,返回类型")public Object handleServiceCall(final ProceedingJoinPoint pjp,最终请求请求,最终类返回类型){ ... }

解决方案

JoinPoint 提到了一个 getSignature() 方法,它的返回类型 Signature 有一个子接口 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天全站免登陆