BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Spring and Boot

Spring

Writing aspects (@Aspect, pointcuts, advice)

An aspect is code that you attach to many methods at once (timing, audit logging, permission checks) without editing them.

Learn these first

At a glance

Mental model

A security gate at the entrance of a building: every visitor passes through it, no matter which office they want.

What happens, in order
  1. 1Mark a class @Aspect and @ComponentA @Pointcut("execution(* com.shop.service..*(..))") names a set of methods, here every method of every class under the service package.
  2. 2@Before, @AfterReturning, @AfterThrowing and @After run around the target in the obvious places@Around receives a ProceedingJoinPoint and decides whether and when to call proceed().
  3. 3Inside advice you can read the method name, arguments and annotations through the JoinPoint, which is how generic audit or timing code knows what it is wrapping
  4. 4Annotation-based pointcuts (@annotation(Audited)) are easier to reason about than name patterns: the method visibly opts in
Remember
Watch out
Forgetting to return proceed() result from @AroundSwallowing exceptions inside advicePointcuts that match far more methods than intendedExpecting advice on private methods or self-calls
Example (highlighted lines explain it)
@Aspect@Componentpublic class TimingAspect {    private static final Logger log = LoggerFactory.getLogger(TimingAspect.class);    @Around("execution(* com.shop.service..*(..))")    public Object time(ProceedingJoinPoint pjp) throws Throwable {        long start = System.nanoTime();        try {            return pjp.proceed();        } finally {            log.info("{} took {} ms", pjp.getSignature(), (System.nanoTime() - start) / 1_000_000);        }    }}
  1. line 1 uses Aspect, the core of Writing aspects (@Aspect, pointcuts, advice)
  2. line 3 uses Aspect, the core of Writing aspects (@Aspect, pointcuts, advice)
  3. line 4 uses Aspect, the core of Writing aspects (@Aspect, pointcuts, advice)
  4. line 6 uses Around, the core of Writing aspects (@Aspect, pointcuts, advice)
  5. line 7 uses ProceedingJoinPoint, the core of Writing aspects (@Aspect, pointcuts, advice)
Read the full explanation (about 1 min)

An aspect is code that you attach to many methods at once (timing, audit logging, permission checks) without editing them. A pointcut says where, and advice says what to do there.

A mental model

A security gate at the entrance of a building: every visitor passes through it, no matter which office they want. The gate is the aspect, the doors are the target methods.

How it works

Mark a class @Aspect and @Component. A @Pointcut("execution(* com.shop.service..*(..))") names a set of methods, here every method of every class under the service package.

@Before, @AfterReturning, @AfterThrowing and @After run around the target in the obvious places. @Around receives a ProceedingJoinPoint and decides whether and when to call proceed().

Inside advice you can read the method name, arguments and annotations through the JoinPoint, which is how generic audit or timing code knows what it is wrapping.

Annotation-based pointcuts (@annotation(Audited)) are easier to reason about than name patterns: the method visibly opts in.

Going deeper

An @Around advice must return the result of proceed() and rethrow exceptions it does not handle. Forgetting to return makes every advised method return null.

Aspects are applied through proxies, so they share the self-invocation limit: a method calling another method of the same class is not advised.

Keep advice cheap and side-effect free where possible. It runs on every matching call, and a slow aspect makes the whole application slow.

Under the hood

The pointcut expression is compiled by AspectJ and matched against each bean method when the proxy is created. The proxy keeps an ordered interceptor chain; proceed() calls the next interceptor, finally the real method. @Order on the aspect controls its position.

Common mistakes

  • Forgetting to return proceed() result from @Around.
  • Swallowing exceptions inside advice.
  • Pointcuts that match far more methods than intended.
  • Expecting advice on private methods or self-calls.

How Unravel recognises it

When you paste code, Unravel looks for these signs of writing aspects (@aspect, pointcuts, advice). Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Writing aspects (@Aspect, pointcuts, advice)?

An aspect is code that you attach to many methods at once (timing, audit logging, permission checks) without editing them. A pointcut says where, and advice says what to do there.

What are the most common mistakes with Writing aspects (@Aspect, pointcuts, advice)?

Forgetting to return proceed() result from @Around. Swallowing exceptions inside advice. Pointcuts that match far more methods than intended. Expecting advice on private methods or self-calls.

How can I see Writing aspects (@Aspect, pointcuts, advice) in real code?

Paste your own Java into Unravel and it will point out where writing aspects (@aspect, pointcuts, advice) appears, explain how it works at the detail level you choose, and draw the flow of the code around it.