In Spring AOP, @Pointcut declares a named, reusable pointcut expression: a rule that selects which method executions an advice such as @Before, @After or @Around applies to. You put it on an empty method inside an @Aspect class, and other advice refers to that method by name instead of repeating the expression.
Spring AOP only supports method execution join points on Spring beans, and it uses a subset of the AspectJ pointcut language. This article shows the syntax, the most useful designators, how to combine pointcuts and the limits you need to know.
AOP separates cross-cutting concerns such as logging, timing, auditing and transactions from business code. Two ideas matter here:
You can write the expression inline in each advice annotation, but then every copy must be edited together. @Pointcut gives the expression a name once, so it can be reused, combined and kept next to the architecture rules it expresses.
The annotated method must have a void return type and an empty body. Only its name, visibility and the annotation value matter.
The expression reads: any method (* return type) in the com.example.shop.service package or its sub-packages (..*) with any arguments ((..)). The aspect class must also be a bean (@Component) and AOP must be enabled, which Spring Boot does automatically when spring-boot-starter-aop is on the classpath. In plain Spring use @EnableAspectJAutoProxy.
Spring AOP supports these designators from AspectJ:
| Designator | Matches | Example |
|---|---|---|
execution | Method execution by signature | execution(public * *(..)) |
within | All methods of types in a package or class | within(com.example.shop.web..*) |
this | Beans whose proxy is of a given type | this(com.example.Auditable) |
target | Beans whose target object is of a given type | target(com.example.Repository) |
args | Methods with matching runtime argument types | args(java.lang.String,..) |
@annotation | Methods that carry an annotation | @annotation(com.example.Audited) |
@within | Methods in classes that carry an annotation | @within(org.springframework.stereotype.Service) |
@target | Target objects whose class has an annotation | @target(com.example.Tracked) |
@args | Arguments whose type has an annotation | @args(com.example.Sensitive) |
bean | Beans by name (Spring specific) | bean(*Service) |
The execution pattern follows this shape: execution(modifiers? return-type declaring-type? method-name(params) throws?). Only the return type, method name and parameters are mandatory.
Pointcuts combine with &&, || and !. Referring to other named pointcuts keeps each expression short.
In XML-free Java config you can also place all pointcuts in a dedicated class, for example SystemArchitecture, and refer to them with a fully qualified name such as "com.example.shop.SystemArchitecture.serviceLayer()". The pointcut method must then be public.
A pointcut can expose values to the advice. Use args and @annotation with parameter names:
The names in the expression must match the parameter names of the pointcut method and of the advice method. This pattern is the usual way to make cross-cutting behaviour opt-in through your own annotation.
this inside the same class bypasses the proxy, so advice does not run for self-invocation.private or final ones.call or get, throw an IllegalArgumentException at startup.execution(* *(..)) in your own package, can advise many beans and add measurable overhead.You can declare it, but it has no effect by itself. It only takes effect when an advice annotation or another pointcut refers to it.
No. The method signature is only a name and a place to hang the annotation, so keep it empty with a void return type.
within selects by type location (package or class), while execution selects by method signature including return type, name and parameters. within is simpler and slightly cheaper to evaluate.
Typical causes: the aspect is not a bean, AOP auto-proxying is not enabled, the target is not a Spring bean, the call is a self-invocation, or the expression does not match the method.
@Pointcut names a reusable expression that decides where advice runs. Keep pointcuts small, give them meaningful names, combine them with &&, || and !, and keep architecture-level rules in one place. Remember that Spring AOP only advises method calls that pass through a proxy.