@Aspect, @Before, @After, @Around — Spring Technical Interview Questions & Answers
Declaring advice with annotations.
@Aspect, @Before, @After, @Around
- You declare aspects with annotations in modern Spring.
- @Aspect marks the aspect class.
- @Before runs before the method.
- @After runs after (more variants exist like after-returning/throwing).
- @Around wraps the call — you can run code before and after proceed().
- Around is powerful; call proceed() or the target never runs.
Technical Interview Tip: Mention proceed() for @Around.
Code Examples for @Aspect, @Before, @After, @Around
@Aspect, @Before, @After, @Around — working example
// Topic: @Aspect, @Before, @After, @Around
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class TopicController {
@GetMapping("/api/topic")
String topic() {
return "Spring Boot: @Aspect, @Before, @After, @Around";
}
}@Aspect, @Before, @After, @Around: GET /api/topic returns this title string.
How to Explain @Aspect, @Before, @After, @Around to an Interviewer
What is @Aspect, @Before, @After, @Around (aop annotations)?
Used for log every service call OrderService Not just the heading
Give one small example of @Aspect, @Before, @After, @Around (aop annotations).
@Aspect + @Around Declaring advice with annotations
What mistake do beginners make with @Aspect, @Before, @After, @Around (aop annotations)?
huge pointcuts that wrap everything Show the fix
Where do you use @Aspect, @Before, @After, @Around (aop annotations)?
OrderService log every service call