michealAtmi
michealAtmi

Reputation: 1042

AOP programming - ProceedingJoinPoint is only supported for around advice

I am having this exception while deploying my new aspect:

@Aspect
public class MyAspect {

    @Before("@annotation(PreAuthorizeAccess)")
    public Object tessst(ProceedingJoinPoint joinPoint) throws Throwable {
        for (Object object : joinPoint.getArgs()) {
            int a = 0;
        }
        return joinPoint.proceed();
    }
}

Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advice at org.springframework.aop.aspectj.AbstractAspectJAdvice.maybeBindProceedingJoinPoint(AbstractAspectJAdvice.java:414) ~[spring-aop-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.aop.aspectj.AbstractAspectJAdvice.calculateArgumentBindings(AbstractAspectJAdvice.java:388) ~[spring-aop-5.1.7.RELEASE.jar:5.1.7.RELEASE]

I want to intercept all methods annotated with @PreAuthorizeAccess and change something in myObject, like this:

@PreAuthorizeAccess
public MyObject save(@Param("argument") MyObject myObject) {

Upvotes: 5

Views: 5802

Answers (1)

Michiel
Michiel

Reputation: 3410

Spring AOP provides multiple pieces of advice, Before and Around among others. You have inadvertently combined the two by setting up a @Before advice and retrieving the method arguments passed to the advice using a ProceedingJoinPoint, which is reserved for the use of an @Around advice. Hence the exception.

From the Spring documentation:

Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

Either declare a before advice using:

@Before("@annotation(PreAuthorizeAccess)")
public void tessst(JoinPoint joinPoint) throws Throwable {
    for (Object object : joinPoint.getArgs()) {
        int a = 0;
    }
}

Or wrap the method invocation in an @Around advice using @Around("@annotation(PreAuthorizeAccess)") instead of @Before("@annotation(PreAuthorizeAccess)").

Although based on the snippets you have provided, my guess is that a @Before advice might be more suitable in your situation.

Upvotes: 5

Related Questions