Understanding Spring AOP’s Four Core Concepts: Aspect, Advice, Pointcut, and Weaving

The article explains Spring AOP’s four fundamental components—pointcut, advice, aspect, and weaving—using a school security analogy and concrete code examples to show how they enable non‑intrusive enhancements such as logging, permission checks, and exception handling in a Spring application.

Java Tech Workshop
Java Tech Workshop
Java Tech Workshop
Understanding Spring AOP’s Four Core Concepts: Aspect, Advice, Pointcut, and Weaving

What is Spring AOP

Spring AOP (Aspect‑Oriented Programming) enables non‑intrusive enhancement of existing code by attaching cross‑cutting concerns (e.g., logging, permission checks, unified exception handling) at specific execution points without modifying the core business logic.

Four core concepts

1. Pointcut – where to enhance

A pointcut defines the join points (methods) that should receive enhancement. It uses expression language to match only the required methods, filtering out the rest.

Common Spring AOP pointcut expressions:

// Match all methods in a specific package (most common)
execution(* com.example.demo.service.*.*(..))

// Match all methods in a specific class
execution(* com.example.demo.service.OrderService.*(..))

// Precisely match a specific method
execution(* com.example.demo.service.OrderService.payOrder(..))

The pointcut only locates the join point; it does not contain the enhancement logic.

2. Advice – what to do

Advice contains the actual enhancement logic that runs at a particular moment of the pointcut method execution.

@Before : runs before the pointcut method (e.g., permission or parameter validation).

@After : runs after the pointcut method regardless of outcome (e.g., logging, resource release).

@AfterReturning : runs after successful completion (e.g., processing the returned result).

@AfterThrowing : runs after an exception is thrown (e.g., error logging, alerts).

@Around : wraps the pointcut method, allowing pre‑, post‑, and exception handling; the most powerful and commonly used.

Example for the payOrder method:

Before advice: verify user login and payment permission.

AfterReturning advice: record payment amount and order number, push to a message queue.

AfterThrowing advice: capture payment failures (e.g., insufficient balance), log the error, and notify the user.

3. Aspect – binding location and action

An aspect combines a pointcut and its associated advice, forming a reusable module.

@Aspect
@Component
public class LogAspect {
    // 1. Define pointcut: match all methods in the service package
    @Pointcut("execution(* com.example.demo.service.*.*(..))")
    public void logPointcut() {}

    // 2. Before advice: log method name and arguments
    @Before("logPointcut()")
    public void beforeLog(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("Before advice: method=" + methodName + ", args=" + java.util.Arrays.toString(args));
    }

    // 3. After advice: indicate method completion
    @After("logPointcut()")
    public void afterLog() {
        System.out.println("After advice: method execution completed");
    }
}

In this example, LogAspect is the aspect; logPointcut() locates all service methods; beforeLog and afterLog provide the logging enhancement.

4. Weaving – when to enhance

Weaving integrates an aspect’s enhancement logic into target objects at a chosen time (compile‑time, class‑loading‑time, or runtime). Spring AOP uses runtime weaving via dynamic proxies (JDK or CGLIB), which is the default and most flexible approach.

Runtime weaving process:

Spring scans for @Aspect -annotated classes at application startup.

For each matching pointcut, Spring creates a proxy object for the target bean.

The proxy executes the configured advice before/after/around the target method.

Example: invoking OrderService.payOrder triggers the before advice (permission check), then the core payment logic, followed by the after advice (logging).

Relationship of the concepts

Pointcut – locates the method to enhance.

Advice – defines the enhancement logic.

Aspect – binds pointcut and advice into a single module.

Weaving – injects the aspect into the target at the chosen time.

Common pitfalls

Confusing pointcut and advice

Incorrect: treating the pointcut as the enhancement logic.

Correct: the pointcut only locates the join point; the advice performs the enhancement. Both must be combined in an aspect.

Forgetting @Component on an aspect

Incorrect: annotating a class only with @Aspect.

Correct: also add @Component so Spring can manage and weave the aspect.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAdviceSpring AOPAspect-Oriented ProgrammingAspectWeavingPointcut
Java Tech Workshop
Written by

Java Tech Workshop

Focused on Java backend technologies, sharing fundamentals, multithreading, JVM, the Spring ecosystem, microservices, distributed systems, high concurrency, source‑code analysis, and practical experience. Continuously delivers high‑quality original content, interview guides, and learning roadmaps to help Java developers progress from beginner to advanced, enhancing technical skills and core competitiveness.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.