Pointcut Expressions — Targeting Methods in Spring AOP
Fast recall
Quick revision
Core answer
X; `@within(X)`/@target(X) — class-level annotations; `args(...)` — argument types/binding; `bean(name)` — Spring-specific, by bean name. Combine with &&, ||, !. Example: execution(* com.example.service.*.*(..)) && @annotation(com.example.Audited) matches every method in the service package annotated with @Audited. Name pointcuts with @Pointcut methods for reuse.Whether you know the primary pointcut designators, AspectJ syntax basics, and the Spring AOP limitation (only method execution join points)
Writing pointcuts that match too broadly (performance hit from advising every method); confusing Spring AOP with full AspectJ (field access, constructor calls not supported in Spring AOP)
Explain why combining designators (e.g. `execution` + `@annotation`) gives both performance and precision; mention that overly broad pointcuts like `execution(* *.*(..))` slow startup because every bean is evaluated
Interview-ready guide
Interview answer
Pointcut expressions are the pattern language that tells Spring AOP which methods to intercept. Get the pattern wrong and your aspect either misses everything, fires on every method in the application, or matches methods in third-party libraries and causes baffling side effects.
`execution()` — match by method signature
- Pattern:
execution(modifiers? return-type declaring-type?.method-name(params)) *matches one path segment;..matches zero-or-more sub-packages in a type pattern and any number of parameters in an arg list- Example:
execution(* com.example.service..*.*(..))matches all methods on all classes inserviceand any sub-package - Without
..for packages, only the exact package is matched — the trap when methods live in sub-packages
`within()` vs `execution()`
within(com.example.service.OrderService)matches by declaring class — cheaper because Spring only needs the class nameexecution(...)evaluates method-by-method and can restrict by return type or parameter types- Prefer
within()for class-level scoping,execution()for narrow method-level matches
`@annotation()` — match by annotation presence
@Around("@annotation(com.example.Timed)")— only methods explicitly annotated are intercepted- The safest approach for custom aspects — explicitly opt-in, no accidental matches on unrelated code
- Lowercase name in the designator binds the annotation instance to a parameter:
@annotation(timed)gives you access totimed.value()in the advice method
`args()` — match and bind arguments
args(order)with a lowercase name binds the matching argument into the advice'sOrder orderparameterargs(com.example.Order)with a fully-qualified type only matches without binding- Combine annotation and args binding in
@Aroundto build configurable aspects
Combining and reusing
- Use
&&,||,!to compose:serviceLayer() && !transactional() - Extract reusable patterns into named
@Pointcutvoid methods
Spring AOP intercepts public methods on Spring-managed beans only. Non-bean objects are never proxied. Private methods are always excluded. Too-broad execution(* *.*(..)) proxies every bean and evaluates every method call — measurable latency at scale.
Pointcut Expressions — Targeting Methods in Spring AOP
Direct Answer
A pointcut is a predicate that selects which join points (method executions in Spring AOP) an aspect should advise. Spring AOP uses AspectJ pointcut syntax but only supports method execution join points (not field access, constructor call). The core designators: execution(...) — match by signature (most common); within(...) — all methods of classes matching a type pattern; @annotation(X) — methods annotated with X; @within(X)/@target(X) — class-level annotations; args(...) — argument types/binding; bean(name) — Spring-specific, by bean name. Combine with &&, ||, !. Example: execution(* com.example.service.*.*(..)) && @annotation(com.example.Audited) matches every method in the service package annotated with @Audited. Name pointcuts with @Pointcut methods for reuse.
Common Mistakes & Pitfalls
Writing pointcuts that match too broadly (performance hit from advising every method); confusing Spring AOP with full AspectJ (field access, constructor calls not supported in Spring AOP)
Check your understanding
Answer these without looking back at the article.
- What's the difference between `within` and `execution` in pointcut expressions?
- Why is `@annotation` preferred over matching by method name for cross-cutting concerns?
- Can Spring AOP intercept private methods or static methods?
- What is the performance cost of a very broad pointcut?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.