What happens if you write a Spring AOP pointcut using an AspectJ designator like call(), get(), or set()?
answer
- Unsupported -> IllegalArgumentException, fail fast
- Supported: execution/within/this/target/args/@target/@args/@within/@annotation + bean
- bean() is Spring-only, not AspectJ
- call=caller side, execution=callee side
- this=proxy type, target=raw type
basics
~20 sSpring rejects it at startup. Using an unsupported designator (call, get, set, initialization, etc.) throws IllegalArgumentException — it is NOT silently ignored. Only Spring's supported subset (execution, within, this, target, args, @annotation, bean, ...) is allowed.
solid answer
~40 sSpring AOP parses your pointcut with AspectJ's expression parser but only accepts the designators its proxy model can honor. If you use one it can't — `call`, `get`, `set`, `initialization`, `preinitialization`, `staticinitialization`, `handler`, `withincode`, `cflow`, `cflowbelow`, `adviceexecution`, or `@this` — Spring throws `IllegalArgumentException` when building the advisor, typically at context startup, with a message listing the supported primitives. So it is a fail-fast error, not a silent no-op. The supported set is: `execution`, `within`, `this`, `target`, `args`, `@target`, `@args`, `@within`, `@annotation`, plus the Spring-only `bean`. In practice you use `execution` for almost everything and combine the others as filters (`within`, `@annotation`, `args`) with `&&`, `||`, `!`.
code
java · 21 lines@Aspect
@Component
public class PointcutDemo {
// OK — supported subset, combined with &&
@Before("execution(* com.acme..*Service.*(..)) && @annotation(com.acme.Audited)")
public void audited() { }
// OK — Spring-only bean() PCD
@Before("bean(orderService)")
public void onOrderService() { }
// FAILS AT STARTUP — IllegalArgumentException:
// 'call' is an AspectJ-only, caller-side designator
// @Before("call(* com.acme.OrderService.place(..))")
// public void broken() { }
// ALSO FAILS — field get/set have no join point in Spring AOP
// @Before("get(int com.acme.Order.total)")
// public void alsoBroken() { }
}go deeper
Know that only certain designators work and that execution is the main one.
Recite the supported set and state that unsupported ones throw IllegalArgumentException at startup.
Explain this/target vs JDK/CGLIB and call vs execution semantics.
Reason about where the parser enforces the subset (AspectJExpressionPointcut) and the fail-fast vs silent-no-match distinction.
## Supported vs unsupported — and the failure mode ### The supported set (memorize this) Spring AOP accepts these AspectJ **pointcut designators (PCDs)**: - `execution(...)` — match method execution join points. The workhorse; you'll use it 90% of the time. - `within(...)` — limit to join points within given types (e.g. `within(com.acme.service..*)`). - `this(...)` — the **proxy** object is an instance of the given type. - `target(...)` — the **target** (raw) object is an instance of the given type. - `args(...)` — the method **arguments** are instances of the given types. - `@target(...)` — the target's **class** carries the given annotation. - `@args(...)` — the runtime **argument types** carry the given annotation. - `@within(...)` — join points within types carrying the given annotation. - `@annotation(...)` — the executing **method** carries the given annotation. Plus one **Spring-specific** PCD that is not AspectJ at all: - `bean(...)` — match by Spring **bean name**, e.g. `bean(*Service)` or `bean(orderService)`. ### The unsupported ones Everything else in AspectJ is rejected: `call`, `get`, `set`, `initialization`, `preinitialization`, `staticinitialization`, `handler`, `withincode`, `cflow`, `cflowbelow`, `adviceexecution`, `if`, `@this`. These correspond to join points (field access, construction, control-flow, call-site) that a proxy simply cannot observe. ### The failure mode — NOT silent A common misconception is that unsupported designators are 'silently ignored.' They are **not**. Spring wraps the AspectJ pointcut parser (`AspectJExpressionPointcut`); when it encounters an unsupported primitive it throws **`IllegalArgumentException`** with a message like: > "...supported Pointcut primitives: [EXECUTION, ARGS, REFERENCE, THIS, TARGET, WITHIN, @ANNOTATION, @WITHIN, @ARGS, @TARGET]" This surfaces as a `BeanCreationException` during application-context startup — **fail fast**. (What IS genuinely silent is a different thing: a validly-parsed pointcut that just never matches — e.g. because of self-invocation or because you targeted a non-bean class — matches zero join points and quietly does nothing.) ### `call` vs `execution` — the classic confusion - `execution(...)` matches at the **callee** side — where the method runs. - `call(...)` (AspectJ only) matches at the **caller** side — where the method is invoked. Spring AOP has no caller-side join point, so `call` is unsupported and you always express things via `execution`. ### `this` vs `target` gotcha With a **JDK dynamic proxy**, the proxy implements the interface but is NOT an instance of the concrete class, so `this(ConcreteClass)` won't match while `target(ConcreteClass)` will. With **CGLIB**, the proxy is a subclass of the concrete class, so both can match. This trips people up when they flip `proxyTargetClass`. ### When to reach for each - Broad, signature-based selection → `execution`. - Package/layer scoping → `within` or `bean`. - Annotation-driven cross-cutting (your own `@Audited`) → `@annotation`. - Passing an argument or annotation into advice → `args` / `@annotation` binding.
- Name the full set of pointcut designators Spring AOP supports.execution, within, this, target, args, @target, @args, @within, @annotation (the AspectJ subset), plus the Spring-specific bean(). Everything else — call, get, set, initialization, cflow, etc. — throws IllegalArgumentException.
- Is an unsupported designator ignored or an error?An error — Spring throws IllegalArgumentException at context startup (fail fast). What's silent is a supported-but-non-matching pointcut, which just advises nothing.
saying these in an interview costs you the question
- Saying unsupported designators are silently ignored
- Listing call/get/set as supported
- Forgetting bean() is Spring-specific and not part of AspectJ