skip to content

Define the core AOP vocabulary: aspect, join point, pointcut, and advice. How do they relate?

level: middleimportance: must knowfreq 85%

answer

  1. aspect = module, join point = a spot, pointcut = filter, advice = deed
  2. Spring join point = method execution only
  3. pointcut expression: execution/within/@annotation/bean
  4. advice types = when: before/after/afterReturning/afterThrowing/around
  5. @Around needs proceed()

basics

~20 s

An aspect is the module holding a cross-cutting concern. A join point is a point where code could run (in Spring, a method call). A pointcut is an expression selecting which join points. Advice is the code that runs at those matched points.

solid answer

~50 s

These four terms are the AOP model. An **aspect** is the modular unit bundling a cross-cutting concern (e.g., a `@Aspect`-annotated class for logging). A **join point** is a well-defined point during program execution where an aspect can plug in — in Spring AOP, always a *method execution* on a Spring bean. A **pointcut** is a predicate/expression (like `execution(* com.app.service.*.*(..))`) that *selects* which join points match. **Advice** is the action taken at a matched join point, and its type says *when* it runs relative to the method: `@Before`, `@After`, `@AfterReturning`, `@AfterThrowing`, and `@Around`. Relationship: an aspect contains one or more advice methods, each bound to a pointcut; when a bean is called, if the call is a join point matched by a pointcut, the associated advice fires. Pointcut = *where*, advice = *what/when*, aspect = the package holding both.

code

java · 14 lines
java
@Aspect
@Component
public class AuditAspect {

    // pointcut: WHICH join points (methods) to match
    @Pointcut("@annotation(com.app.Audited)")
    void auditedMethods() {}

    // advice bound to the pointcut; JoinPoint exposes the matched call
    @AfterReturning(pointcut = "auditedMethods()", returning = "result")
    public void record(JoinPoint jp, Object result) {
        audit.log(jp.getSignature().getName(), jp.getArgs(), result);
    }
}

go deeper

for a junior

Should give a one-line definition of each of the four terms.

for a middle

Must correctly relate them and know Spring join points are method executions only, plus name the advice types.

for a senior

Comfortable with pointcut designators (@annotation, execution, within) and why @Around is powerful but should be used sparingly.

for a principal

Can contrast Spring's proxy model with AspectJ's richer join-point model and reason about the trade-offs.

## The four building blocks Think of it as a sentence: **an aspect says 'run this advice at the join points selected by this pointcut.'** ### 1. Aspect The **modular unit** that encapsulates a cross-cutting concern. In Spring you write it as a Spring bean annotated with `@Aspect` (from AspectJ's annotation set) plus `@Component` so it's picked up. It groups related pointcuts and advice. ```java @Aspect @Component public class LoggingAspect { ... } ``` ### 2. Join point A **point during the execution of a program** where an aspect *can* be applied. AspectJ defines many kinds (method call, method execution, field get/set, constructor, static initializer...). **Spring AOP supports exactly one kind: method execution** on a Spring-managed bean, because it's proxy-based. So in Spring, 'join point' effectively means 'a call to a public method of a bean.' The `JoinPoint` object (or `ProceedingJoinPoint` for around advice) is passed into advice to expose the signature, arguments, and target. ### 3. Pointcut A **predicate that matches join points** — it answers 'which methods should this advice apply to?' Expressed with the AspectJ **pointcut expression language**. Common designators: - `execution(...)` — match method executions by signature (most used). - `within(...)` — match all join points within given types/packages. - `@annotation(...)` — match methods carrying a given annotation (how `@Transactional` targeting works). - `bean(...)` — match by Spring bean name. - `args(...)`, `this(...)`, `target(...)` — match by argument types or proxy/target types. You can name a pointcut with `@Pointcut` and reuse it: ```java @Pointcut("execution(* com.app.service..*(..))") void serviceMethods() {} ``` ### 4. Advice The **action** an aspect takes at a matched join point. The *type of advice* determines *when* it runs: | Advice | Annotation | Runs | |---|---|---| | Before | `@Before` | before the method | | After returning | `@AfterReturning` | after normal return (can read the return value) | | After throwing | `@AfterThrowing` | after the method throws | | After (finally) | `@After` | after the method regardless of outcome | | Around | `@Around` | wraps the method; must call `ProceedingJoinPoint.proceed()` | `@Around` is the most powerful: it can run code before *and* after, alter arguments, change/replace the return value, swallow or translate exceptions, and short-circuit by not calling `proceed()`. Because it fully controls execution, prefer weaker advice types when they suffice. ### How they fit together ```java @Aspect // (1) the aspect @Component public class LoggingAspect { @Pointcut("execution(* com.app.service..*(..))") // (3) pointcut: WHERE void serviceMethods() {} @Around("serviceMethods()") // (4) advice: WHAT/WHEN, bound to pointcut public Object logTiming(ProceedingJoinPoint pjp) throws Throwable { // (2) join point long t = System.nanoTime(); try { return pjp.proceed(); } finally { log.info("{} took {}ns", pjp.getSignature(), System.nanoTime() - t); } } } ``` ### Common confusions - **Join point vs pointcut**: a join point is a *single* candidate point in execution; a pointcut is an *expression selecting a set* of join points. Many candles (join points); the pointcut picks which ones to light. - **Advice vs aspect**: advice is one piece of behavior; an aspect can contain several advices plus pointcuts. - **Spring's limitation**: only method-execution join points; you cannot advise field access or constructors with Spring AOP (you'd need full AspectJ weaving). ### Mnemonic **A**spect = the module, **J**oin point = a spot in execution, **P**ointcut = the filter, **A**dvice = the deed.

  • What kinds of join points does Spring AOP support, and why only those?
    Only method executions on Spring beans. Spring AOP is proxy-based: it wraps a bean in a proxy that intercepts method calls, so it can only advise methods — not field access, constructors, or static initializers. Full AspectJ (compile/load-time weaving) supports those.
  • When would you choose @Around over @Before?
    When you need to run code after the method too, measure/modify the return value, alter arguments, handle or translate exceptions, or conditionally skip the method (by not calling proceed()). If you only need to act beforehand, @Before is simpler and safer.

saying these in an interview costs you the question

  • Saying a join point and a pointcut are the same thing.
  • Claiming Spring AOP can advise field access or constructor calls (it can't — proxy-based, method-execution only).
  • Forgetting that @Around must call proceed() or the target method never runs.
  • Thinking 'advice type' controls WHERE it applies — it controls WHEN; the pointcut controls where.

context