Backend Development 9 min read

Master 10 Essential Spring AOP Pointcut Expressions in Spring Boot 3

This article introduces ten common Spring AOP pointcut expressions, explains their syntax and usage with both annotation‑based and XML configurations, provides concrete code examples, and shows how to combine them for flexible aspect weaving in Spring Boot 3.4.2.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master 10 Essential Spring AOP Pointcut Expressions in Spring Boot 3

Environment

SpringBoot 3.4.2

1. Introduction

Spring AOP (Aspect‑Oriented Programming) is a powerful feature of the Spring framework that allows developers to modularize cross‑cutting concerns such as logging and transaction management without touching business logic. Pointcut expressions define when and how an aspect is applied.

Pointcut expressions consist of a specific syntax that can precisely specify which methods are intercepted, based on method signatures, annotations, return types, parameter types, etc. For example, execution(* com.pack..*.*(..)) matches all methods in the com.pack package and its sub‑packages, while @annotation(com.pack.PackLog) matches methods annotated with @PackLog .

This article introduces ten common Spring AOP pointcut expressions.

2. Practical Cases

Based on Annotations

<code>@Pointcut("within(@com.pack.business.PackLog *)")
public void logMethodExecution() {}
</code>

Use the defined pointcut in advice:

<code>@Around("logMethodExecution()")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
    // ...
}
</code>

Based on XML

<code>&lt;aop:config&gt;
    &lt;!-- Define a pointcut that matches all methods of classes in a specific package --&gt;
    &lt;aop:pointcut id="repositoryMethods" expression="execution(* com.pack.business.repository..*(..))"/&gt;
    &lt;aop:aspect ref="myAspectBean"&gt;
        &lt;aop:before method="beforeAdvice" pointcut-ref="repositoryMethods"/&gt;
    &lt;/aop:aspect&gt;
&lt;/aop:config&gt;
</code>

2.1 execution

<code>@Pointcut("execution(public String com.pack.service.UserService.findById(Long))")
</code>

This matches the execution of findById in UserService . A more flexible version uses wildcards:

<code>@Pointcut("execution(* com.pack.service.UserService.*(..))")
</code>

2.2 within

<code>@Pointcut("within(com.pack.service.UserService)")
</code>

Or any type in the package:

<code>@Pointcut("within(com.pack..*)")
</code>

2.3 this & target

this matches join points where the bean reference is an instance of a given type (CGLIB proxy); target matches where the target object implements a given interface (JDK proxy).

<code>@Pointcut("target(com.pack.dao.BaseDAO)")
</code>
<code>@Pointcut("this(com.pack.aop.UserDAO)")
</code>

2.4 args

<code>@Pointcut("execution(* *..find*(Long))")
</code>

Matches methods starting with find that have a single Long argument. To allow additional arguments:

<code>@Pointcut("execution(* *..find*(Long,..))")
</code>

2.5 @target

<code>@Pointcut("@target(com.pack.annotation.PackLog)")
</code>

2.6 @args

<code>@Pointcut("@args(com.pack.annotation.Entity)")
public void entityArgs() {}
</code>
<code>@Before("entityArgs()")
public void entityBefore(JoinPoint jp) {
    logger.info("@Entity annotation: " + jp.getArgs()[0]);
}
</code>

2.7 @within

<code>@Pointcut("@within(com.pack.annotation.PackLog)")
</code>

Equivalent to:

<code>@Pointcut("within(@com.pack.annotation.PackLog *)")
</code>

2.8 @annotation

<code>@Pointcut("@annotation(com.pack.annotation.PackLog)")
public void logMethod() {}
</code>

2.9 bean

<code>@Pointcut("bean(*Service)")
</code>

Matches all beans whose name ends with Service . Specific examples:

<code>@Pointcut("bean(userService)")
</code>
<code>@Pointcut("bean(com.pack.service.*)")
</code>

2.10 Combined Pointcuts

<code>@Pointcut("bean(*Service) || bean(*DAO)")
</code>

Matches any method in beans whose name ends with Service or DAO .

JavaBackend DevelopmentSpring BootSpring AOPPointcut Expressions
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.