What is Spring AOP and how do aspects, advice, and pointcuts work?
Understand Spring AOP and how aspects, advice, and pointcuts use proxies to modularize logging, security, and transactions, with clear examples.
Expected Interview Answer
Spring AOP is a framework for aspect-oriented programming that lets you modularize cross-cutting concerns like logging, security, and transactions by weaving extra behavior into methods without editing their code.
An aspect is a module bundling a cross-cutting concern; a pointcut is an expression that selects which join points (method executions) it applies to; and advice is the code that runs at those points, such as @Before, @After, @Around, @AfterReturning, and @AfterThrowing. Spring implements AOP with runtime proxies (JDK dynamic proxies or CGLIB), so calls to a bean pass through a proxy that invokes the matching advice around the real method.
- Keeps business logic free of boilerplate concerns
- Centralizes logging, security, and transaction handling
- Reduces code duplication across many methods
- Enables clean separation of concerns
- Lets you change cross-cutting behavior in one place
AI Mentor Explanation
Think of an umpire who automatically checks for a no-ball on every single delivery without the bowler or batter arranging it. The pointcut is the rule 'every delivery', the advice is the no-ball check, and the umpire is the aspect. Spring AOP works the same way: your method is the delivery, and the framework injects the check around it invisibly on every call.
Step-by-Step Explanation
Step 1
Enable AOP
Add spring-boot-starter-aop; @EnableAspectJAutoProxy is auto-configured so aspects are detected.
Step 2
Declare an aspect
Annotate a Spring bean with @Aspect and @Component so the container registers it.
Step 3
Write a pointcut
Use an expression like execution(* com.app.service.*.*(..)) to select the join points to advise.
Step 4
Add advice
Attach @Before, @Around, @AfterReturning, or @AfterThrowing methods bound to the pointcut.
Step 5
Let Spring weave
Spring creates a JDK or CGLIB proxy for the bean so calls flow through the advice at runtime.
What Interviewer Expects
- Clear definitions of aspect, pointcut, join point, and advice
- Knowledge of the five advice types
- Understanding that Spring AOP is proxy-based, not full AspectJ weaving
- Awareness that AOP applies at method execution join points only
- Common use cases like logging, security, and transactions
Common Mistakes
- Confusing a pointcut (the selector) with advice (the code that runs)
- Expecting AOP to intercept self-invocation calls within the same bean
- Thinking Spring AOP can advise fields or constructors like full AspectJ
- Forgetting @Around advice must call proceed() to run the target method
- Not knowing proxies require the bean to be a Spring-managed component
Best Answer (HR Friendly)
“Spring AOP lets developers add shared behavior like logging or security to many methods in one place instead of copying it everywhere. You define which methods to target and what extra code should run, and Spring automatically applies it around those methods.”
Code Example
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.app.service.*.*(..))")
public void serviceMethods() {}
@Around("serviceMethods()")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
long took = System.currentTimeMillis() - start;
System.out.println(pjp.getSignature() + " took " + took + "ms");
return result;
}
}Follow-up Questions
- What is the difference between @Around and @Before advice?
- How does Spring choose between JDK dynamic proxies and CGLIB?
- Why does calling a method on 'this' bypass the aspect?
- What is a join point versus a pointcut?
- How does @Transactional rely on Spring AOP under the hood?
MCQ Practice
1. In Spring AOP, what does a pointcut define?
A pointcut is an expression that selects the join points (method executions) where advice should be applied.
2. Which advice type can control whether the target method runs at all?
@Around wraps the method and only proceeds if it calls proceed(), so it can prevent or alter the invocation.
3. How does Spring AOP apply advice at runtime?
Spring AOP uses runtime proxies (JDK dynamic proxies or CGLIB) that intercept method calls to the bean.
Flash Cards
What is an aspect? — A module that bundles a cross-cutting concern (like logging) together with the advice and pointcuts that apply it.
What is a pointcut? — An expression that selects which join points (method executions) advice should run on.
What is advice? — The code executed at a matched join point: @Before, @After, @Around, @AfterReturning, or @AfterThrowing.
How does Spring AOP work internally? — It wraps beans in JDK dynamic or CGLIB proxies that invoke advice around the real method call.
Why is self-invocation not advised? — Internal calls bypass the proxy, so the aspect never intercepts them.
Continue Learning
Related Interview Questions
What is dependency injection in Spring and how does the IoC container work?
medium
What is a Spring profile and how do you configure environment-specific beans?
medium
What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
easy
What is the difference between @Controller and @RestController in Spring?
easy