Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Master 10 Essential Spring AOP Pointcut Expressions in Spring Boot 3

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.

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

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

Use the defined pointcut in advice:

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

Based on XML

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

2.1 execution

@Pointcut("execution(public String com.pack.service.UserService.findById(Long))")

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

@Pointcut("execution(* com.pack.service.UserService.*(..))")

2.2 within

@Pointcut("within(com.pack.service.UserService)")

Or any type in the package:

@Pointcut("within(com.pack..*)")

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

@Pointcut("target(com.pack.dao.BaseDAO)")
@Pointcut("this(com.pack.aop.UserDAO)")

2.4 args

@Pointcut("execution(* *..find*(Long))")

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

@Pointcut("execution(* *..find*(Long,..))")

2.5 @target

@Pointcut("@target(com.pack.annotation.PackLog)")

2.6 @args

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

2.7 @within

@Pointcut("@within(com.pack.annotation.PackLog)")

Equivalent to:

@Pointcut("within(@com.pack.annotation.PackLog *)")

2.8 @annotation

@Pointcut("@annotation(com.pack.annotation.PackLog)")
public void logMethod() {}

2.9 bean

@Pointcut("bean(*Service)")

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

@Pointcut("bean(userService)")
@Pointcut("bean(com.pack.service.*)")

2.10 Combined Pointcuts

@Pointcut("bean(*Service) || bean(*DAO)")

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

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaBackend DevelopmentSpring BootSpring AOPpointcut expressions

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion