Question - What is an advice? Explain its types in spring.
Answer -
An advice is the implementation of cross-cutting concerns can be applied to other modules of the spring application. Advices are of mainly 5 types:
Before:
- This advice executes before a join point, but it does not have the ability to prevent execution flow from proceeding to the join point (unless it throws an exception).
- To use this, use @Before annotation.
AfterReturning:
- This advice is to be executed after a join point completes normally i.e if a method returns without throwing an exception.
- To use this, use @AfterReturning annotation.
AfterThrowing:
- This advice is to be executed if a method exits by throwing an exception.
- To use this, use @AfterThrowing annotation.
After:
- This advice is to be executed regardless of the means by which a join point exits (normal return or exception encounter).
- To use this, use @After annotation.
Around:
- This is the most powerful advice surrounds a join point such as a method invocation.
- To use this, use @Around annotation.