What does the execution() pointcut designator match, and how would you read execution(public * com.app.service..*.*(..))?
answer
- modifiers ret-type decl-type name(params) throws
- = one, .. = many
- method-execution join point only
- single dot ≠ sub-packages
- proxy => public + Spring bean
basics
~10 sexecution() matches method executions. That expression means: any public method, returning any type, in any class inside com.app.service or its sub-packages, with any name and any number of arguments.
solid answer
~40 sexecution() is the most common AspectJ pointcut designator. In Spring AOP it selects method-execution join points — i.e. the running of a matching method on a Spring bean. Reading execution(public * com.app.service..*.*(..)) left to right: public = the method must be public; * = return type is anything; com.app.service.. = the declaring type lives in com.app.service or any sub-package (.. means 'this package and below'); the next * = any class; .* = any method name; (..) = any parameter list, zero or more of any type. So it advises every public method of every class under the service package tree. You typically pair it with an advice annotation like @Around or @Before in an @Aspect class.
code
java · 10 lines@Aspect
@Component
public class ServiceLoggingAspect {
// Any public method under com.app.service (and sub-packages)
@Before("execution(public * com.app.service..*.*(..))")
public void logEntry(JoinPoint jp) {
System.out.println("Entering " + jp.getSignature().toShortString());
}
}go deeper
Should read a pointcut and explain each token; know * vs .. at a basic level.
Should know execution is method-execution-only and proxy-based; know the sub-package trap.
Should articulate the full grammar and combine with other designators.
Should reason about proxy limitations, self-invocation, and CGLIB vs JDK implications on what execution can actually advise.
## What execution() is `execution()` is a **pointcut designator** — a mini-expression language (borrowed from AspectJ) that tells Spring AOP *which join points to intercept*. A **join point** is a point in program execution where an aspect can run; in Spring AOP the only supported join point is **method execution**, so `execution()` is the workhorse designator. You put it inside an advice annotation (`@Before`, `@After`, `@Around`, etc.) or a `@Pointcut` method, inside a class annotated `@Aspect`. ## The example, term by term `execution(public * com.app.service..*.*(..))` | Token | Meaning | |-------|---------| | `public` | Modifier pattern — method must be public | | `*` (first) | Return-type pattern — any return type | | `com.app.service..` | Declaring-type/package pattern; `..` = this package **and all sub-packages** | | `*` (after `..`) | Any class/type name | | `.*` | Any method name | | `(..)` | Parameter pattern — `..` = any number of parameters of any type (including zero) | Read as: *advise any public method, of any class, anywhere under `com.app.service`, regardless of name, return type, or arguments.* ## Two wildcards - `*` — matches **one** element: one return type, one method-name segment, one type-name segment, or exactly **one** parameter. - `..` — matches **zero or more**: in a package position it spans nested packages; in the parameter position it spans any number of parameters. ## Where it runs Spring AOP is **proxy-based**. It wraps your bean in a proxy; when an *external* caller invokes a matched method the proxy runs the advice. This means `execution()` only advises **Spring-managed beans**, effectively **public** methods, and **not** self-invocation (a method calling another method on `this` bypasses the proxy). ## When to use Use `execution()` when you want to select methods by their **signature** (package, class, name, args). Prefer `within()` for coarse type-level matching and `@annotation`/`@within` for marker-annotation matching, but `execution()` is the most precise and most used. ## Common gotcha People expect `com.app.service.*` (single dot + `*`) to include sub-packages — it does **not**. Only `com.app.service..` (double dot) descends into sub-packages.
- Does com.app.service.* match classes in com.app.service.impl?No. A single dot with * only matches types directly in com.app.service. To include sub-packages you must use the double-dot form com.app.service.. (as in ..*.*).
- What kinds of join points does execution() match in Spring AOP?Only method executions. Spring AOP does not support field access, constructor call, or call join points that full AspectJ offers — Spring AOP is proxy-based and method-execution only.
saying these in an interview costs you the question
- Thinking * matches multiple packages/parameters (that is what .. does)
- Believing com.app.service.* descends into sub-packages
- Claiming execution() can match field access or constructor calls in Spring AOP