skip to content

execution() Designator

execution() matches method signatures, with wildcards for return type, package, class, name and parameters. Writing one correctly for something like 'all public methods in the service package' is a standard live exercise.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

questions

5

What does the execution() pointcut designator match, and how would you read execution(public * com.app.service..*.*(..))?

level: juniorimportance: must knowfreq 62%

answer

  1. modifiers ret-type decl-type name(params) throws
  2. = one, .. = many
  3. method-execution join point only
  4. single dot ≠ sub-packages
  5. proxy => public + Spring bean

basics

~10 s

execution() 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 s

execution() 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
java
@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

for a junior

Should read a pointcut and explain each token; know * vs .. at a basic level.

for a middle

Should know execution is method-execution-only and proxy-based; know the sub-package trap.

for a senior

Should articulate the full grammar and combine with other designators.

for a principal

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

context

open as a page

Break down the full grammar of an execution() pointcut. Which parts are optional, and which are required?

level: middleimportance: must knowfreq 48%

basics

~10 s

Grammar: execution(modifiers? ret-type decl-type? name(params) throws?). Required are the return-type pattern, the method-name pattern, and the parameter pattern. Optional are modifiers, the declaring-type prefix, and the throws clause.

open as a page

Explain the difference between the * and .. wildcards in an execution() pointcut, giving package, type, method-name, and parameter examples.

level: middleimportance: should knowfreq 44%

basics

~10 s
  • matches exactly one item (one name segment, one type, one parameter). .. matches zero or more — in package position it spans sub-packages, in the parameter position it spans any number of arguments.
open as a page

A colleague writes execution(* com.app..*.*(..)) but their advice never fires on some methods. What proxy-related reasons could explain this, given how Spring AOP evaluates execution() pointcuts?

level: seniorimportance: should knowfreq 34%

basics

~20 s

Spring AOP is proxy-based, so execution() only advises calls that go through the proxy: external calls to Spring-managed beans. Self-invocation (this.method()), private/final methods, and non-bean objects won't be intercepted even if the pointcut technically matches.

open as a page

As a lead, when would you choose execution() over within(), this(), target(), or @annotation for a pointcut — and how do you keep execution() expressions maintainable across a large codebase?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Use execution() when you need signature-level precision (return type, name, args). Use within() for coarse type/package scope, @annotation for marker-driven cross-cutting, and this()/target() for proxy/target type checks. Keep it maintainable by centralizing named @Pointcut methods and composing them.

open as a page