What is Advice in Spring AOP?
In Spring AOP, Advice is the action or logic that executes at a specific point during method execution.
In simple words:
βAdvice defines what should happen and when it should happen in AOP.β
Why Advice is Needed
Enterprise applications often require common functionalities such as:
- Logging
- Security checks
- Transaction management
- Performance monitoring
- Exception handling
Without advice:
- Developers write repeated code
- Business logic becomes messy
- Maintenance becomes difficult
Advice helps centralize such common functionality.
Real-Time Example
Suppose:
- You want to log every service method call
Instead of writing logging code inside every method, Spring AOP advice automatically executes logging logic.
Simple Logging Example Without AOP
public void saveStudent() {
System.out.println("Method Started");
// Business Logic
System.out.println("Method Ended");
}
Problem Without Advice
- Repeated logging code
- Poor maintainability
- Code duplication
Solution Using Advice
Advice automatically executes:
- Before method
- After method
- When exception occurs
Main Types of Advice in Spring AOP
| Advice Type | Description |
|---|---|
| @Before | Runs before method execution |
| @After | Runs after method execution |
| @AfterReturning | Runs after successful execution |
| @AfterThrowing | Runs when exception occurs |
| @Around | Runs before and after method |
1. @Before Advice
@Before advice executes:
- Before target method runs
@Before Example
@Before(
"execution(* com.example.service.*.*(..))"
)
public void logBefore() {
System.out.println(
"Method Started"
);
}
Real-Time Use Case
- Logging
- Authentication checks
- Input validation
@Before Execution Flow
Advice Executes
β
Target Method Executes
2. @After Advice
@After advice executes:
- After target method finishes
- Whether success or exception occurs
@After Example
@After(
"execution(* com.example.service.*.*(..))"
)
public void logAfter() {
System.out.println(
"Method Finished"
);
}
Real-Time Use Case
- Cleanup operations
- Closing resources
@After Flow
Target Method Executes
β
Advice Executes
3. @AfterReturning Advice
@AfterReturning advice executes:
- Only after successful method execution
@AfterReturning Example
@AfterReturning(
pointcut =
"execution(* com.example.service.*.*(..))",
returning = "result"
)
public void logResult(
Object result
) {
System.out.println(
"Returned: " + result
);
}
Real-Time Use Case
- Success logging
- API response monitoring
@AfterReturning Flow
Target Method Executes Successfully
β
Advice Executes
4. @AfterThrowing Advice
@AfterThrowing advice executes:
- When exception occurs
@AfterThrowing Example
@AfterThrowing(
pointcut =
"execution(* com.example.service.*.*(..))",
throwing = "ex"
)
public void logException(
Exception ex
) {
System.out.println(
ex.getMessage()
);
}
Real-Time Use Case
- Error logging
- Alert systems
- Failure monitoring
@AfterThrowing Flow
Target Method Throws Exception
β
Advice Executes
5. @Around Advice
@Around advice executes:
- Before method
- After method
It provides full control over method execution.
@Around Example
@Around(
"execution(* com.example.service.*.*(..))"
)
public Object monitorExecution(
ProceedingJoinPoint joinPoint
) throws Throwable {
long start =
System.currentTimeMillis();
Object result =
joinPoint.proceed();
long end =
System.currentTimeMillis();
System.out.println(
"Execution Time: "
+ (end - start)
);
return result;
}
Real-Time Use Case
- Performance monitoring
- Execution timing
- Caching
- Security
@Around Flow
Before Advice
β
Target Method Executes
β
After Advice
Complete Logging Example
Service Class
@Service
public class StudentService {
public void saveStudent() {
System.out.println(
"Student Saved"
);
}
}
Aspect Class
@Aspect
@Component
public class LoggingAspect {
@Before(
"execution(* com.example.service.*.*(..))"
)
public void logBefore() {
System.out.println(
"Method Started"
);
}
}
Output
Method Started
Student Saved
What is Pointcut in Advice?
Advice uses:
Pointcut Expressions
to define target methods.
Pointcut Example
execution(* com.example.service.*.*(..))
What is Join Point?
Join point is:
- The method execution point where advice runs
How Spring Executes Advice
Spring AOP uses:
- Dynamic proxies
to intercept method calls and apply advice.
Spring AOP Execution Flow
Client Request
β
Spring Proxy
β
Advice Executes
β
Target Method Executes
Advantages of Advice
- Reduces duplicate code
- Improves maintainability
- Separates business logic
- Centralized logging and security
- Cleaner application design
Disadvantages of Excessive Advice Usage
- Complex debugging
- Runtime overhead
- Harder understanding for beginners
Real-Time Use Cases of Advice
- API logging
- Authentication
- Authorization
- Performance tracking
- Audit logging
- Transaction management
Difference Between Aspect and Advice
| Feature | Aspect | Advice |
|---|---|---|
| Definition | Class containing AOP logic | Action executed by aspect |
| Example | LoggingAspect | @Before method |
Common Interview Questions on Advice
What is advice in Spring AOP?
Advice is the action executed at specific join points.
What are the types of advice?
@Before, @After, @AfterReturning, @AfterThrowing, and @Around.
Which advice provides full control over method execution?
@Around advice.
Which advice runs only after successful execution?
@AfterReturning.
Which advice handles exceptions?
@AfterThrowing.
Conclusion
Advice is one of the core concepts in Spring AOP.
It helps developers:
- Apply common functionalities automatically
- Reduce duplicate code
- Improve maintainability
- Build clean enterprise applications
Understanding advice is essential for Spring Boot developers because enterprise applications heavily rely on logging, security, monitoring, transactions, and auditing.
Proper use of advice improves:
- Code organization
- Scalability
- Maintainability
- Application reliability