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