Spring AOP Aspects, Pointcuts, Advice, Weaving is an important Spring topic because it appears in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.
For this page, focus on what problem Spring AOP Aspects, Pointcuts, Advice, Weaving solves, where developers usually make mistakes, and how to verify the result. The audit note for this lesson was: under 650 content words; limited checklist/practice/mistake/FAQ notes .
A strong understanding of Spring AOP Aspects, Pointcuts, Advice, Weaving should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work with tools or output.
Spring AOP Aspects Pointcuts Advice Weaving should be studied as a practical Spring lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the spring > aop page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
Aspect-Oriented Programming (AOP) is a programming paradigm that allows you to separate cross-cutting concerns (like logging, security, transactions) from your business logic. Instead of scattering the same code across many classes, you define it once in an Aspect.
| Term | Description |
|---|---|
| Aspect | A class containing cross-cutting logic (annotated with @Aspect) |
| Advice | The action taken at a join point (@Before, @After, @Around, etc.) |
| Join Point | A point in program execution (method call, exception throw) |
| Pointcut | An expression that matches join points (which methods to intercept) |
| Weaving | Linking aspects with application objects (Spring does this at runtime) |
package com.example.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
// Pointcut: matches all methods in service package
@Pointcut("execution(* com.example.service.*.*(..))")
public void serviceLayer() {}
// @Before: runs BEFORE the method
@Before("serviceLayer()")
public void logBefore(JoinPoint joinPoint) {
System.out.println("[BEFORE] " + joinPoint.getSignature().getName()
+ " called with args: " + java.util.Arrays.toString(joinPoint.getArgs()));
}
// @After: runs AFTER the method (regardless of outcome)
@After("serviceLayer()")
public void logAfter(JoinPoint joinPoint) {
System.out.println("[AFTER] " + joinPoint.getSignature().getName() + " completed");
}
// @AfterReturning: runs after successful return
@AfterReturning(pointcut = "serviceLayer()", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("[AFTER_RETURNING] " + joinPoint.getSignature().getName()
+ " returned: " + result);
}
// @AfterThrowing: runs when method throws exception
@AfterThrowing(pointcut = "serviceLayer()", throwing = "ex")
public void logAfterThrowing(JoinPoint joinPoint, Exception ex) {
System.err.println("[AFTER_THROWING] " + joinPoint.getSignature().getName()
+ " threw: " + ex.getMessage());
}
// @Around: wraps the method - most powerful advice
@Around("execution(* com.example.service.*.*(..)) && @annotation(com.example.annotation.Timed)")
public Object measureTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
try {
Object result = joinPoint.proceed(); // Execute the actual method
long duration = System.currentTimeMillis() - start;
System.out.println("[TIMED] " + joinPoint.getSignature().getName()
+ " took " + duration + "ms");
return result;
} catch (Exception e) {
System.err.println("[TIMED] " + joinPoint.getSignature().getName()
+ " failed after " + (System.currentTimeMillis() - start) + "ms");
throw e;
}
}
}
@Aspect
@Component
public class PointcutExamples {
// All methods in UserService
@Pointcut("execution(* com.example.service.UserService.*(..))")
public void userServiceMethods() {}
// All public methods in any class
@Pointcut("execution(public * *(..))")
public void publicMethods() {}
// Methods starting with "get"
@Pointcut("execution(* com.example..get*(..))")
public void getterMethods() {}
// Methods with @Transactional annotation
@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")
public void transactionalMethods() {}
// All beans in service package
@Pointcut("within(com.example.service.*)")
public void servicePackage() {}
// Combine pointcuts
@Pointcut("servicePackage() && publicMethods()")
public void publicServiceMethods() {}
// Use combined pointcut
@Before("publicServiceMethods()")
public void logPublicServiceCall(JoinPoint jp) {
System.out.println("Calling: " + jp.getSignature());
}
// Transaction aspect example
@Around("@annotation(com.example.annotation.RequiresTransaction)")
public Object manageTransaction(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Starting transaction...");
try {
Object result = pjp.proceed();
System.out.println("Committing transaction...");
return result;
} catch (Exception e) {
System.out.println("Rolling back transaction...");
throw e;
}
}
}
When studying Spring AOP Aspects, Pointcuts, Advice, Weaving, separate three things: the concept, the syntax, and the situation where it is useful. This prevents the lesson from becoming a list of commands with no practical meaning.
In Spring, Spring AOP Aspects, Pointcuts, Advice, Weaving becomes easier when you build a tiny example first, then increase complexity. Add one realistic input, one invalid or boundary input, and one explanation of why the result changes.
class SpringAOPAspectsPointcutsAdviceWeavingReview {
public static void main(String[] args) {
String state = "ready";
System.out.println("Spring AOP Aspects Pointcuts Advice Weaving: " + state);
}
}
String value = null;
if (value == null) {
System.out.println("Spring AOP Aspects Pointcuts Advice Weaving: handle the missing value before continuing");
}
Memorizing Spring AOP Aspects Pointcuts Advice Weaving without the situation where it is useful.
Connect Spring AOP Aspects Pointcuts Advice Weaving to a concrete Spring task.
Testing Spring AOP Aspects Pointcuts Advice Weaving only with the perfect input.
Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Changing code before reading the visible symptom or error message.
Inspect the output, state, configuration, or stack trace connected to Spring AOP Aspects Pointcuts Advice Weaving.
Memorizing Spring AOP Aspects Pointcuts Advice Weaving without the situation where it is useful.
Connect Spring AOP Aspects Pointcuts Advice Weaving to a concrete Spring task.
The common mistake is memorizing syntax without understanding when the behavior changes or fails.
Remember the problem it solves in Spring, then attach the syntax or steps to that problem.
You can predict the result of a small example, explain a failure case, and choose it over a nearby alternative for a clear reason.
They often copy the syntax but skip the state, input, dependency, selector, route, type, or configuration that controls the behavior.
Explore 500+ free tutorials across 20+ languages and frameworks.