Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Describe the five advice types in Spring AOP and when each runs.

Spring AOP has five advice types. @Before runs before the method and can stop it only by throwing an exception, @AfterReturning runs after a normal return and can see the return value, and @AfterThrowing runs when the method throws and can see the exception. @After runs after the method in every case, like a finally block, and @Around wraps the whole call: it must call ProceedingJoinPoint.proceed() to run the method and can change the arguments, the return value or the exception.

Why this question is asked

Advice types are the vocabulary of every aspect we write, and choosing the wrong one is a common source of subtle bugs: an @Around that forgets to return the result, an @After that tries to read a return value, or an @Before that was supposed to skip the method. Interviewers ask this question to check that we know exactly when each one runs and what it can and cannot do. In this answer, we will recap what advice is, go through the five types with one running example, look at the order in which they fire, and finish with how to choose between them.

1A Quick Recap of Advice

In aspect-oriented programming, advice is the code an aspect runs at a selected point in the program. In Spring AOP, those points are always method executions on Spring beans, selected by a pointcut expression. The advice runs inside the proxy that Spring puts in front of the bean, so it can act before the real method runs, after it, or around it.

Each advice method can take a JoinPoint parameter, which describes the call: the method signature, the arguments and the target object. @Around advice takes a ProceedingJoinPoint instead, a JoinPoint with one extra ability, proceed(), which runs the real method.

2The Running Example

To compare the types, we will advise one method with all five and see what each prints. The method divides two numbers, so it is easy to make it either return normally or throw.

Java
@Service
class Calculator {
public int divide(int a, int b) { return a / b; }
}
@Aspect
@Component
class Tracing {
@Before("execution(* Calculator.divide(..))")
void before(JoinPoint jp) { log.info("before {}", Arrays.toString(jp.getArgs())); }
@AfterReturning(pointcut = "execution(* Calculator.divide(..))", returning = "result")
void afterReturning(Object result) { log.info("returned {}", result); }
@AfterThrowing(pointcut = "execution(* Calculator.divide(..))", throwing = "ex")
void afterThrowing(ArithmeticException ex) { log.info("threw {}", ex.getMessage()); }
@After("execution(* Calculator.divide(..))")
void after() { log.info("after (always)"); }
@Around("execution(* Calculator.divide(..))")
Object around(ProceedingJoinPoint pjp) throws Throwable {
log.info("around: start");
try { return pjp.proceed(); }
finally { log.info("around: end"); }
}
}
// calculator.divide(10, 2) prints:
// around: start | before [10, 2] | returned 5 | after (always) | around: end
// calculator.divide(1, 0) prints:
// around: start | before [1, 0] | threw / by zero | after (always) | around: end

Notice that in the second call, @AfterReturning did not run and @AfterThrowing did, while @After ran in both. The ordering of the lines inside one aspect is the one Spring documents since Spring Framework 5.2.7: around wraps everything, then before, then the method, then after-returning or after-throwing, then after.

3@Before: Runs First, Stops Only by Throwing

@Before advice runs before the method and has no handle to the method's execution. It can read the arguments through JoinPoint, but it cannot replace them, and it cannot decide to skip the method by returning something. The only way it can stop the call is by throwing an exception, which then propagates to the caller and the method never runs. That makes it a natural fit for validation and simple permission checks: if the check fails, throw; otherwise do nothing and the call continues.

4@AfterReturning and @AfterThrowing: One Outcome Each

@AfterReturning runs only when the method returns normally. With returning = "result" we bind the return value to a parameter. The parameter type also filters: a parameter of type Order only matches methods that return an Order. The advice can read the value and even mutate an object it refers to, but it cannot replace the returned reference.

@AfterThrowing runs only when the method throws. With throwing = "ex" we bind the exception, and again the parameter type filters, so ArithmeticException ex runs only for that type and its subclasses. It is good for logging or translating failures into metrics. It cannot swallow the exception: after the advice runs, the exception continues to the caller, unless the advice itself throws a different one, which replaces it.

5@After: Like a finally Block

@After, sometimes called after-finally advice, runs after the method whatever the outcome, exactly like a finally block. It sees neither the return value nor the exception, because it must work in both cases. It is the right place for cleanup that must always happen, such as clearing a thread-local context set in @Before, or recording that a call finished.

6@Around: Full Control of the Call

@Around is the most powerful type. The advice method receives a ProceedingJoinPoint and decides whether and how to run the real method by calling proceed(). It can call proceed(newArgs) with different arguments, skip proceed() entirely and return a cached value, call it several times to retry, catch an exception and return a fallback, or change the return value. Because the method's result comes back from proceed(), the advice must declare Object as its return type and return that result; an @Around advice declared void makes every advised method return null.

With that power comes responsibility: an @Around that forgets to call proceed() silently disables the method, and one that catches Throwable can hide real failures. The usual advice is to use the least powerful type that does the job.

The five advice types
TypeRunsCan seeCan change
@Beforebefore the methodargumentsnothing; can only throw to stop the call
@AfterReturningafter a normal returnarguments, return valuenot the returned reference
@AfterThrowingwhen the method throwsarguments, exceptioncan throw a different exception
@Afteralways, like finallyargumentsnothing
@Aroundaround the whole calleverythingarguments, result, exception, whether it runs
Choose the least powerful type that does the job.

7Follow-up questions

In what order do advice from different aspects run?

By the aspects' order, set with @Order or the Ordered interface. On the way in, the aspect with the highest precedence (the lowest order value) runs first; on the way out, it runs last, so the highest-precedence aspect wraps all the others. Without an explicit order, the order between aspects is undefined.

Can @AfterReturning change the result?

It cannot replace the returned reference, because it has no way to hand back a different object. If the returned object is mutable, the advice can change its contents, but that is surprising for readers. When the result must change, use @Around.

Why use @Before instead of @Around for a check?

Because @Before cannot forget to call proceed() or lose the return value. It does less, so there is less to get wrong, and the intent, check first, is clearer to the reader.

8Conclusion

In this answer, we saw the five advice types in Spring AOP. @Before runs first and can stop the call only by throwing. @AfterReturning runs after a normal return and can read the result, and @AfterThrowing runs on an exception and can read it. @After always runs, like finally. @Around wraps the call, must call proceed() and return its result, and can change arguments, results and exceptions. Choosing the least powerful type that does the job keeps aspects safe and readable.

What to say in the interviewAbout 90 seconds aloud

Open with

There are five. @Before runs before the method. It can read the arguments through the JoinPoint, but it cannot change them or skip the method, except by throwing an exception, so it is good for validation and simple checks. @AfterReturning runs only when the method returns normally, and with the returning attribute I can bind the return value; the parameter type also filters which methods it applies to. @AfterThrowing runs only when the method throws, and with the throwing attribute I can bind the exception; it cannot swallow it, the exception still reaches the caller unless the advice throws a different one. @After runs in every case, like a finally block, so it sees neither the result nor the exception and is meant for cleanup.

Then, if they want more

And @Around wraps the whole call: it gets a ProceedingJoinPoint, and it must call proceed to run the method and return its result, so it has to be declared as returning Object. Because it controls the call, it can change arguments, return a cached value without proceeding, retry, translate exceptions or change the result.

Within one aspect the order is around, then before, the method, after-returning or after-throwing, then after. Across aspects it is decided by @Order. I usually pick the least powerful type that does the job.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Describe the five advice types in Spring AOP and when each runs.": https://scrivio-interview-library.netlify.app/spring-boot/aop/467ca27d64/