Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring aop pointcut expression to access method return type

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){ ... }
like image 524
Sean Patrick Floyd Avatar asked Jun 23 '10 08:06

Sean Patrick Floyd


People also ask

Is return type mandatory in pointcut expression?

Syntax of pointcut expressionThe return-type-pattern, method-name-pattern, and param-pattern are mandatory fields, and the remaining fields are optional.

What method does this pointcut expression reference?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

What is pointcut expression in spring?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.

Which type of advice allows to access the return value of a JoinPoint?

Join point information is available in advice bodies by declaring a parameter of type org. aspectj. lang. JoinPoint.


1 Answers

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.

like image 151
Christian Semrau Avatar answered Oct 29 '22 15:10

Christian Semrau