Spring AOP (Aspect-Oriented Programming)
In enterprise apps, you have certain tasks that happen across <em>every</em> layer: logging, security checks, and performance metrics. If you write <code>logger.info()</code> inside every single controller and service method, your code becomes a messy tangled rope. <strong>Spring AOP</strong> solves this by letting you intercept methods without touching their code.
The Hyderabad Metro Security Check Analogy:
Imagine if every passenger on the Hyderabad Metro had to carry their own personal metal detector and scan themselves before sitting down. That would be chaotic and inefficient.
Instead, the Metro stations have a centralized security checkpoint. Every passenger (method) must pass through this checkpoint (Aspect) before boarding the train (executing). The passenger doesn't know exactly how the scan works; they just walk through.
In Spring Boot, an Aspect is that centralized security checkpoint. You write a logging or security rule in one place, and Spring automatically applies it to all your methods.
- @Aspect: Marks a class as an Aspect.
- @Before / @After: Advice that runs before or after a method executes.
- @Around: The most powerful advice. It wraps the method completely. You can decide whether to let the method run, modify its arguments, or skip it entirely.
- ProceedingJoinPoint: The object you use inside
@Aroundto actually execute the original method viaproceed().
Add the AOP dependency to pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>The Aspect Code (LoggingAspect.java)
package com.devinhyderabad;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
// 1. Intercept ALL methods inside the com.devinhyderabad package
@Around("execution(* com.devinhyderabad..*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
// 2. Let the actual method run
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
// 3. Log which method took how long
System.out.println("Method " + joinPoint.getSignature().getName() +
" executed in " + duration + "ms");
return result;
}
}Now, every time any method in your app is called, this Aspect will print exactly how many milliseconds it took, without you writing a single line of logging inside your controllers.
Interview Question: "What is Spring AOP used for, and what are some common use cases?"
Answer: AOP is used to separate cross-cutting concerns (like logging, security, transaction management, and caching) from business logic. Instead of writing logging code in 100 different methods, you write one Aspect. Spring uses AOP under the hood for @Transactional and @Async.
Enterprise Note: AOP is powerful but can be a double-edged sword. Because it operates invisibly via proxies, if an Aspect throws an unexpected error, it can be very hard for developers to debug why their method isn't being called. Always document your Aspects clearly.
Key Takeaways
- ✅ AOP separates cross-cutting concerns (logging, security) from business logic
- ✅ @Around is the most powerful advice — wraps the entire method execution
- ✅ ProceedingJoinPoint.proceed() lets the original method run
- ✅ Spring uses AOP internally for @Transactional and @Async
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login