Spring AOP Basics: Cross-Cutting Concerns

Harry · 11 Sep 2026 · 11 views

Spring AOP Basics: Cross-Cutting Concerns

Aspect-oriented programming (AOP) extracts behavior that repeats across many methods - logging, transactions, security - into reusable modules called aspects. Spring AOP implements this with dynamic proxies, so no code generation or bytecode weaving is needed for most applications.

The vocabulary

An aspect is a module of cross-cutting behavior. A pointcut selects where behavior applies using expressions over method signatures. An advice is the code that runs at those points, with before, after, and around variants. A join point is each method eligible for interception.

A practical aspect

@Aspect
@Component
public class LoggingAspect {
    @Around("execution(* com.example.service.*.*(..))")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.nanoTime();
        Object result = pjp.proceed();
        long elapsed = System.nanoTime() - start;
        System.out.println(pjp.getSignature() + " took " + elapsed + " ns");
        return result;
    }
}

When you are actually using it

You use AOP even if you never write an aspect: @Transactional and @Secured are advice applied through proxy-based interception. When Spring creates a transactional service, it gives you a proxy that opens, commits, or rolls back the transaction around your method calls.

Limits of proxy AOP

Spring AOP only intercepts calls that come through the proxy. An internal call from one method of the same class to another bypasses the proxy, so the advice does not run. That is why self-invocation is a classic gotcha with @Transactional.

Key Points

  • AOP extracts repeated cross-cutting behavior into aspects.
  • Pointcuts select methods; advice implements before, after, or around logic.
  • @Transactional and security annotations are AOP under the hood.
  • Spring AOP uses proxies, so internal self-calls are not intercepted.
  • Aspects help keep services focused on business logic.
Share this post:

Comments (0)

Please login or register to comment.