Mona D
Mona D

Reputation: 15

Pointcut expression returning throws error

I have defined a pointcut like this

@Pointcut("execution(* com.personal.services.Example.buildList(..))")
public void contextInterceptor() {
//pointcut identifier
}

I want to use it for afterReturning advice. How do I add the returning variable so that I can access it in my advice?

@AfterReturning("contextInterceptor()")
public Object contextAdvice(JoinPoint jp, Object returnObj){
//process returnobj;
return returnObj;
}

I tried this but it gives me error

@Pointcut("execution(* com.personal.services.Example.buildList(..))",returning="returnObj")

Upvotes: 0

Views: 95

Answers (2)

BillFromHawaii
BillFromHawaii

Reputation: 334

You can do it all in one method like this:

@AfterReturning(
    pointcut = "execution(* com.personal.services.Example.buildList(..))",
    returning = "retVal"
)
public void afterReturning(JoinPoint joinPoint, Object retVal) {
    if (retObject != null) {
        logger.error("Returned object: " + retVal);
        logger.error("Returned type:   " + retVal.getClass().getName());
    }
}

Upvotes: 1

Mona D
Mona D

Reputation: 15

I got it. I am doing this and its now working fine.

@AfterReturning(pointcut="contextInterceptor()",returning="returnObj")

Upvotes: 0

Related Questions