![](/img/trans.png)
[英]How to intercept the return type List using the spring AOP pointcut expression
[英]Spring aop pointcut expression to access method return type
我有一個帶有許多方法的服務接口,所有這些方法都接受一個Request對象並返回一個Response對象。 所有請求對象都有一個共同的祖先,所有響應對象都有一個不同的共同祖先(它有一個成功標志和一個消息字段)。
現在,我希望有一個檢查權限等的方面,執行服務調用並返回帶有失敗代碼的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){ ... }
JoinPoint
的Javadoc提到了一個getSignature()
方法,其返回類型Signature
有一個你可以嘗試轉換的子接口MethodSignature
,它有一個getReturnType()
方法,可能就是你要找的方法。
您可以在around advice方法(在您的情況下為handleServiceCall())內進行檢查
Object actuals = pjp.proceed();
if(actuals instanceof MyResponse){
//TO:DO: Your code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.