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 :
<code>execution(modifiers-pattern? ret-type-pattern? declaring-type-pattern?name-pattern(param-pattern?) throws-pattern?)</code>Example:
<code>@Pointcut("execution(public com.pack.Person com.pack.PersonService.query*(..))")</code>Advice types are explained with code snippets:
Before advice runs before the matched method. <code>// @Before example @Before("recordLog()") public void beforeLog() { // ... } public void beforeLog(JoinPoint jp) { } public void beforeLog(JoinPoint.StaticPart jp) { }</code>
After returning advice runs after a method returns successfully. <code>@AfterReturning(value = "recordLog()", returning = "ret") public void afterReturningLog(Object ret) { System.out.printf("Returned value: %s%n", ret); }</code>
After throwing advice runs when a method throws an exception. <code>@AfterThrowing(value = "recordLog()", throwing = "ex") public void afterThrowingLog(Throwable ex) { System.err.printf("Exception: %s%n", ex.getMessage()); }</code>
After (finally) advice runs regardless of method outcome. <code>@After("recordLog()") public void after(JoinPoint jp) { System.out.println("Final advice"); }</code>
Around advice wraps the method execution. <code>@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; }</code>
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:
<code>@Before("bean(*Service) && args(id)")
public void beforeLog(JoinPoint jp, Long id) { }
</code>Inter‑type declarations ( @DeclareParents ) allow adding new interfaces to existing classes at runtime:
<code>@Aspect
public class LogAspect {
@DeclareParents(value = "com.pack.aop.create.AspectTest.PersonService", defaultImpl = DefaultDAO.class)
private DAO dao;
}</code>Images illustrate the AOP concepts and example outputs:
The article concludes with a reminder that the content is free and will be continuously updated.
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.