The @Pointcut annotation declares a named, reusable pointcut expression in Spring AOP. You put it on an empty method inside an @Aspect class, and the expression says which method executions an advice should intercept. Advice annotations such as @Before, @Around and @AfterReturning then refer to that method by name instead of repeating the expression.
@Pointcut comes from AspectJ (org.aspectj.lang.annotation.Pointcut). Spring uses the AspectJ pointcut expression language, but it applies it with proxy-based AOP, so only method executions on Spring-managed beans can be matched.
An aspect has two parts: a pointcut (where to apply behaviour) and advice (what to run). Without @Pointcut, every advice repeats its own expression string. That is hard to read and easy to get out of sync.
A @Pointcut method has a void return type and an empty body. The method name is the pointcut name, and its parameters can bind values such as arguments for use in advice.
The method body is never called. Spring only reads the expression and the method name. The visibility of the method controls where you can reference it: public pointcuts can be used from other aspects, private ones only inside the same class.
The expression is built from designators. Spring AOP supports a subset of AspectJ; designators it does not support throw an IllegalArgumentException at startup.
| Designator | Matches |
|---|---|
execution(...) | Method execution by modifier, return type, declaring type, name and parameters. The most common one. |
within(...) | All methods of the given type or package. |
this(...) | The proxy is an instance of the given type. |
target(...) | The target object is an instance of the given type. |
args(...) | The runtime arguments are of the given types. Also binds them to advice parameters. |
@annotation(...) | The method carries the given annotation. |
@within(...) | The declaring class carries the given annotation. |
@target(...) | The target class carries the given annotation. |
@args(...) | The runtime argument types carry the given annotation. |
bean(...) | A Spring bean by name, with wildcards (Spring-specific). |
The shape of execution is: execution(modifiers? return-type declaring-type? name(params) throws?). Examples:
execution(* com.example.shop.service.*.*(..)) matches any method of any class directly in the service package.execution(* com.example.shop.service..*.*(..)) includes sub-packages.execution(* *..OrderService.place*(String, ..)) matches methods starting with place whose first parameter is a String.Pointcut expressions combine with &&, || and !. Named pointcuts can reference each other, which lets you build small, readable pieces.
Keep a central class of such pointcuts for the architecture of the application, and keep the business-specific aspects small.
Reference the pointcut by its method name. If it lives in another class, qualify it with the class name.
The aspect itself is a Spring bean, so it needs @Component (or a @Bean method). @Aspect alone does not register it.
Pointcut methods can declare parameters that are bound from the join point. This avoids digging through JoinPoint.getArgs().
Add the AOP starter. Spring Boot then enables proxy-based AspectJ support automatically, so you do not need @EnableAspectJAutoProxy unless you use plain Spring without Boot.
Check the Spring Boot release notes for your version, because starter names can change between major releases. Outside Boot, add spring-aop and aspectjweaver and put @EnableAspectJAutoProxy on a configuration class.
new are not matched. Full AspectJ with compile-time or load-time weaving covers those.this, the call bypasses the proxy, so the advice does not run. Move the called method to another bean, or inject the proxy.final classes and final or private methods cannot be proxied by CGLIB, so they are not advised.execution expressions are resolved when the bean is created, so a typo in a package name silently matches nothing. Test your pointcuts, for example with a small integration test that asserts the advice ran.No. It must return void and is normally empty. The method only provides a name and, optionally, parameters that bind values for the advice.
Yes, but it does nothing on its own. A pointcut becomes active only when an advice annotation refers to it.
execution matches method signatures, including name, return type and parameters. within matches every join point inside a type or package, which is simpler when you only care about location.
Common causes are a wrong package in the expression, a target class that is not a Spring bean, a call through this instead of the proxy, or a missing @Component on the aspect.
@Pointcut gives a name to a pointcut expression so you can reuse and combine it across advice. Write it on an empty method in an @Aspect class, combine small pointcuts with &&, || and !, and reference them by name from @Before, @Around and the other advice annotations. Remember that Spring AOP is proxy-based: it only sees method calls to Spring beans that pass through the proxy.