Master Spring AOP: Real‑World AspectJ Pointcut & Advice Examples in Spring Boot 3
This comprehensive guide walks you through Spring Boot 3 AOP fundamentals, showing how to declare aspects with @Aspect, define pointcuts using @Pointcut, apply various advice types, manage ordering, bind method parameters, and leverage inter‑type declarations with clear code examples and diagrams.
Spring Boot 3 practical AOP examples are presented, starting with the declaration of an aspect using the @Aspect annotation, which allows developers to define cross‑cutting concerns such as logging, transaction management, and security.
Pointcuts are defined with @Pointcut, using AspectJ pointcut expression language. The most commonly used expressions are execution and @annotation:
execution(modifiers-pattern? ret-type-pattern? declaring-type-pattern?name-pattern(param-pattern?) throws-pattern?)Example:
@Pointcut("execution(public com.pack.Person com.pack.PersonService.query*(..))")Advice types are explained with code snippets:
Before advice runs before the matched method.
// @Before example
@Before("recordLog()")
public void beforeLog() {
// ...
}
public void beforeLog(JoinPoint jp) { }
public void beforeLog(JoinPoint.StaticPart jp) { }After returning advice runs after a method returns successfully.
@AfterReturning(value = "recordLog()", returning = "ret")
public void afterReturningLog(Object ret) {
System.out.printf("Returned value: %s%n", ret);
}After throwing advice runs when a method throws an exception.
@AfterThrowing(value = "recordLog()", throwing = "ex")
public void afterThrowingLog(Throwable ex) {
System.err.printf("Exception: %s%n", ex.getMessage());
}After (finally) advice runs regardless of method outcome.
@After("recordLog()")
public void after(JoinPoint jp) {
System.out.println("Final advice");
}Around advice wraps the method execution.
@Around("recordLog()")
public Object aroundLog(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Before execution");
Object ret = pjp.proceed();
System.out.println("After execution");
return ret;
}Advice ordering can be controlled with @Order or by implementing the Ordered interface. When multiple advices of the same type exist in the same aspect, ordering cannot be set via @Order, but ordering across different aspects can be managed by annotating the aspect class.
Parameter binding is demonstrated using JoinPoint and args(...) expressions:
@Before("bean(*Service) && args(id)")
public void beforeLog(JoinPoint jp, Long id) { }Inter‑type declarations ( @DeclareParents) allow adding new interfaces to existing classes at runtime:
@Aspect
public class LogAspect {
@DeclareParents(value = "com.pack.aop.create.AspectTest.PersonService", defaultImpl = DefaultDAO.class)
private DAO dao;
}Images illustrate the AOP concepts and example outputs:
The article concludes with a reminder that the content is free and will be continuously updated.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
