Advice in AOP
AOP (Aspect-Oriented Programming) helps developers keep cross-cutting concerns like logging, security, or performance monitoring separate from main business logic.
In Spring AOP, Advice is the block of code that runs before, after, or around specific methods (called join points).
This means you can add useful features to your app without modifying existing code.
Types of Advice in Spring AOP with Examples
1. Before Advice
In Spring AOP, Advice is the block of code that runs before, after, or around specific methods (called join points).
This means you can add useful features to your app without modifying existing code.
Types of Advice in Spring AOP with Examples
1. Before Advice
When: Runs before the target method is executed.
Use case: Logging, security checks, input validation.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;
@Aspectpublic class LoggingAspect {
@Before("execution(* com.aop.aopexample.service.MyService.*(..))") public void beforeAdvice() { System.out.println("Before Advice Executed!!"); }}
Output
Before Advice Executed!!
Inside doSomethingMethod!!
2. After Returning Advice
When: Runs after the target method finishes successfully.
Use case: Logging returned data, data transformation.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;
@Aspectpublic class LoggingAspect {
@AfterReturning(pointcut = "execution(* com.aop.aopexample.service.MyService.*(..))", returning = "result") public void afterReturningAdvice(Object result) { System.out.println("AfterReturning Advice Executed!!"); }}
Output
Inside doSomethingMethod!!
AfterReturning Advice Executed!!
3. After Throwing Advice
When: Runs if the target method throws an exception.
Use case: Error logging, alerting, cleanup.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;
@Aspectpublic class LoggingAspect {
@AfterThrowing(pointcut = "execution(* com.aop.aopexample.service.MyService.*(..))", throwing = "exception") public void afterThrowingAdvice(Exception exception) { System.out.println("AfterThrowing Advice Executed!!"); }}
Output
Exception in thread "main" AfterThrowing Advice Executed!!java.lang.ArithmeticException: / by zero
4 After (Finally) Advice
When: Runs after the target method finishes whether it throws an exception or not.
Use case: Releasing resources, logging end of process.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;
@Aspectpublic class LoggingAspect {
@After("execution(* com.aop.aopexample.service.MyService.*(..))") public void afterAdvice() { System.out.println("After Advice Executed!!"); }}
Output
Inside doSomethingMethod!!
After Advice Executed!!
5. Around Advice
When: Wraps around the target method runs before and after, and can even skip or change the method call.
Use case: Measuring execution time, caching, transactions.
package com.aop.aopexample.aspect;
import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.ProceedingJoinPoint;
@Aspectpublic class LoggingAspect {
@Around("execution(* com.aop.aopexample.service.MyService.*(..))") public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); // call the original method long endTime = System.currentTimeMillis();
String methodName = joinPoint.getSignature().toShortString(); long executionTime = endTime - startTime;
System.out.println(methodName + " executed in " + executionTime + " ms"); return result; }}
Output
Inside doSomethingMethod!!MyService.doSomething() executed in 6 ms
Using Advice in Spring AOP helps keep your code clean and modular by moving repeated logic (logging, security, timing, etc.) into separate classes, without changing your main business methods.