Bird
Raised Fist0
Spring Bootframework~10 mins

Pointcut expressions in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Pointcut expressions
Start: Define Pointcut Expression
↓
Match Join Points in Code
↓
Apply Advice at Matched Points
↓
Execute Advice
↓
Continue Program Execution
Pointcut expressions select specific points in the program (join points) where advice runs, guiding when extra code executes.
Execution Sample
Spring Boot
execution(* com.example.service.*.*(..))
This pointcut matches execution of any method in any class inside com.example.service package.
Execution Table
StepPointcut ExpressionJoin Point EvaluatedMatch ResultAdvice Action
1execution(* com.example.service.*.*(..))com.example.service.UserService.getUser()MatchAdvice runs before getUser()
2execution(* com.example.service.*.*(..))com.example.service.OrderService.placeOrder()MatchAdvice runs before placeOrder()
3execution(* com.example.service.*.*(..))com.example.repository.UserRepository.save()No MatchAdvice does not run
4execution(* com.example.service.*.*(..))com.example.controller.UserController.listUsers()No MatchAdvice does not run
5execution(* com.example.service.*.*(..))End of join pointsStopNo more advice applications
💡 All join points checked; advice applied only where pointcut matches methods in com.example.service package.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Join PointNoneUserService.getUser()OrderService.placeOrder()UserRepository.save()UserController.listUsers()Checked all
Match ResultNoneMatchMatchNo MatchNo MatchFinal results recorded
Advice AppliedNoneYesYesNoNoAdvice applied only on matches
Key Moments - 2 Insights
Why does advice run for UserService methods but not for UserRepository methods?
Because the pointcut expression matches only methods inside the com.example.service package, not com.example.repository, as shown in execution_table rows 1-3.
What happens if a method signature changes but stays in the same package?
The pointcut still matches because it uses wildcards (*) and (..), so any method name and parameters in the package are matched, as seen in rows 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, does advice run for UserController.listUsers()?
ANo, advice does not run
BYes, advice runs
COnly sometimes
DDepends on method parameters
💡 Hint
Check row 4 in the execution_table where UserController.listUsers() is evaluated.
At which step does the pointcut expression stop checking join points?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look at the exit_note and row 5 in the execution_table.
If the pointcut expression changed to include com.example.repository package, what would happen at step 3?
AError occurs
BMatch and advice runs
CNo match and no advice
DAdvice runs only sometimes
💡 Hint
Refer to variable_tracker and execution_table row 3 for current behavior and imagine the package inclusion.
Concept Snapshot
Pointcut expressions select where advice runs in Spring AOP.
Syntax example: execution(* package.Class.method(..))
Wildcards * and .. match any method or parameters.
Only matched join points trigger advice.
Used to modularize cross-cutting concerns like logging.
Full Transcript
Pointcut expressions in Spring Boot define which methods or join points advice should apply to. The expression uses patterns like execution(* com.example.service.*.*(..)) to match all methods in a package. During execution, each join point is checked against the pointcut. If it matches, advice runs before or after that method. If not, advice is skipped. This lets developers add behavior like logging or security checks only where needed. Wildcards help match many methods easily. Understanding which methods match helps avoid confusion about when advice runs.

Practice

(1/5)
1. What does a pointcut expression in Spring Boot primarily do?
easy
A. Create new methods dynamically at runtime
B. Define the database connection settings
C. Select methods where extra code (advice) should run
D. Replace existing methods with new implementations

Solution

  1. Step 1: Understand the purpose of pointcut expressions

    Pointcut expressions specify where additional code, called advice, should be applied in existing methods without changing those methods.
  2. Step 2: Differentiate from other concepts

    They do not create or replace methods, nor configure databases. Their role is to select join points (methods) for advice.
  3. Final Answer:

    Select methods where extra code (advice) should run -> Option C
  4. Quick Check:

    Pointcut expression = Select methods for advice [OK]
Hint: Pointcuts pick methods to add code without changing them [OK]
Common Mistakes:
  • Confusing pointcuts with method creation
  • Thinking pointcuts configure databases
  • Believing pointcuts replace method code
2. Which of the following is the correct syntax to match all methods in package com.example.service using a pointcut expression?
easy
A. execution(* com.example.service..*(..))
B. execution(com.example.service.*.*(..))
C. execution(* com.example.service.*(..))
D. execution(* com.example.service.*.*(..))

Solution

  1. Step 1: Understand the execution syntax

    The syntax execution(* com.example.service.*.*(..)) matches any return type, any class in the package com.example.service, and any method with any parameters.
  2. Step 2: Analyze other options

    execution(com.example.service.*.*(..)) misses the wildcard for return type. execution(* com.example.service..*(..)) uses double dot .. which matches subpackages, not just the package. execution(* com.example.service.*(..)) misses the class wildcard.
  3. Final Answer:

    execution(* com.example.service.*.*(..)) -> Option D
  4. Quick Check:

    Use * for any class and method in package [OK]
Hint: Use * for any class and method in a package [OK]
Common Mistakes:
  • Omitting the return type wildcard *
  • Using .. instead of * for classes
  • Missing method wildcard *
3. Given the pointcut expression execution(public * com.example.repo.*Repository.find*(..)), which methods will it match?
medium
A. All methods named 'find' in any class in com.example.repo
B. All public methods starting with 'find' in any class ending with 'Repository' in package com.example.repo
C. All public methods in any class in com.example.repo
D. Only methods named exactly 'find' in any class ending with 'Repository'

Solution

  1. Step 1: Break down the expression

    public * means any public return type, com.example.repo.*Repository matches classes ending with 'Repository' in the package, and find*(..) matches methods starting with 'find' with any parameters.
  2. Step 2: Match options to expression meaning

    All public methods starting with 'find' in any class ending with 'Repository' in package com.example.repo correctly describes all public methods starting with 'find' in classes ending with 'Repository' in the package. Others are either too broad or too narrow.
  3. Final Answer:

    All public methods starting with 'find' in any class ending with 'Repository' in package com.example.repo -> Option B
  4. Quick Check:

    execution(public * com.example.repo.*Repository.find*(..)) = All public methods starting with 'find' in any class ending with 'Repository' in package com.example.repo [OK]
Hint: Look for wildcards * and method name patterns carefully [OK]
Common Mistakes:
  • Ignoring method name prefix 'find*'
  • Confusing class name pattern *Repository
  • Missing public modifier effect
4. Identify the error in this pointcut expression: execution(* com.example..service.*(..)
medium
A. Missing closing parenthesis at the end
B. Incorrect use of double dots '..' in package name
C. Wildcard '*' cannot be used for method names
D. Return type '*' is invalid

Solution

  1. Step 1: Check syntax completeness

    The expression is missing a closing parenthesis ')' at the end, which is required to close the execution() expression.
  2. Step 2: Validate other parts

    Double dots '..' are valid to match subpackages, '*' is valid for method names, and '*' is valid for return type.
  3. Final Answer:

    Missing closing parenthesis at the end -> Option A
  4. Quick Check:

    Check parentheses balance in execution() [OK]
Hint: Count parentheses carefully in pointcut expressions [OK]
Common Mistakes:
  • Forgetting closing parenthesis
  • Misunderstanding '..' usage
  • Thinking '*' is invalid for methods or return types
5. You want to create a pointcut expression that matches all methods in any class within com.app.service and its subpackages, but only methods that have exactly two parameters of any type. Which expression is correct?
hard
A. execution(* com.app.service..*.*(*, *))
B. execution(* com.app.service.*.*(..))
C. execution(* com.app.service..*.*(.., ..))
D. execution(* com.app.service..*.*(*))

Solution

  1. Step 1: Understand package and subpackage matching

    com.app.service..* matches classes in the package and all subpackages.
  2. Step 2: Match method parameters exactly

    (* , *) means exactly two parameters of any type. (..) means any number of parameters, so it's incorrect here.
  3. Step 3: Analyze options

    execution(* com.app.service..*.*(*, *)) correctly matches all classes in package and subpackages with exactly two parameters. Others either match any number of parameters or wrong parameter counts.
  4. Final Answer:

    execution(* com.app.service..*.*(*, *)) -> Option A
  5. Quick Check:

    Use (*, *) for exactly two parameters [OK]
Hint: Use (*, *) to match exactly two parameters [OK]
Common Mistakes:
  • Using (..) to match exact parameter count
  • Confusing single * with .. for subpackages
  • Missing commas between parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard