cross-cutting

AOP & Aspects

Apply cross-cutting concerns (logging, security, metrics, retries) declaratively without scattering them through every method.

Some concerns — logging, security checks, metrics, transactions, retries — are needed in many places but belong to none of them. Scattering that code through every method tangles the cross-cutting concern with the business logic and duplicates it endlessly. Aspect-Oriented Programming factors those concerns out into one place and weaves them back in declaratively, so the business method stays clean and the cross-cutting behaviour is defined and changed in a single location.

Recording every phone call without modifying the phones — the line itself captures and routes.

Key Concepts

1
In Spring AOP the vocabulary is specific. An aspect is the module holding the cross-cutting logic. A join point is a point in execution where it could apply — in Spring, always a method execution. A pointcut is an expression selecting which join points to target (for example, every method in a service package, or every method annotated @Loggable). Advice is the code that runs at a matched join point, and its type fixes the timing: @Before, @AfterReturning, @AfterThrowing, @After, and the most powerful @Around, which wraps the call and can inspect arguments, short-circuit it, modify the result, or add retry and timing logic. Spring implements this with runtime proxies — it wraps your bean in a proxy that runs the advice around the real method call. This is exactly how @Transactional, @Cacheable, and method security are themselves implemented.
@Loggable@Before@AfterReturning@AfterThrowing@After
2
Because it is proxy-based, Spring AOP carries the same caveats as @Transactional: it only intercepts calls that go through the proxy, so self-invocation within the same bean bypasses the advice, and by default it applies to Spring-managed beans' public methods. Interviewers like to connect AOP to those annotations — recognising that the framework's own declarative features are aspects — and to note that for finer-grained or non-method join points you would step up to full AspectJ with compile- or load-time weaving.
@Transactional
3
What the interviewer is probing.
What the interviewer is probing.
4
1. "What is AOP for?" *Probing:* the purpose. *Stalls:* "Logging." *Moves up:* cross-cutting concerns — transactions, security, caching, metrics — applied without scattering the same code through every method.
1. "What is AOP for?"
5
2. "How does Spring implement it?" *Probing:* the mechanism. *Stalls:* "Bytecode." *Moves up:* proxies — JDK dynamic proxies for interfaces or CGLIB subclasses — which is exactly why self-invocation is not intercepted.
2. "How does Spring implement it?"
6
3. "What is a pointcut?" *Probing:* the vocabulary. *Stalls:* "A method." *Moves up:* the expression selecting which join points an advice applies to.
3. "What is a pointcut?"
7
4. "When is a custom aspect a bad idea?" *Probing:* judgement. *Stalls:* "Never." *Moves up:* when it hides business behaviour — invisible logic that changes results is very hard for the next person to find.
4. "When is a custom aspect a bad idea?"