Loading

Quipoin Menu

Learn • Practice • Grow

spring / Pointcut Expressions
tutorial

Pointcut Expressions

Imagine you are a security guard and you have been told: "Check everyone who enters through the main door between 9 AM and 5 PM." This is a pointcut expression – it specifies exactly which joinpoints to target.

In Spring AOP, pointcut expressions use AspectJ syntax. The most common is execution().

Execution Expression Syntax:
execution(modifiers-pattern? return-type-pattern declaring-type-pattern? method-name-pattern(param-pattern) throws-pattern?)

Here are practical examples:


// Any public method in any class
@Before("execution(public * *(..))")

// Any method in com.example.service package
@Before("execution(* com.example.service.*.*(..))")

// Any method that starts with 'get'
@Before("execution(* get*(..))")

// Any method that takes a String parameter
@Before("execution(* *(String))")

// Any method in BankService class regardless of parameters
@Before("execution(* com.example.service.BankService.*(..))")

// Any method with any number of parameters
@Before("execution(* *(..))")

Other pointcut designators:
  • within() – limits to certain types.
    @Before("within(com.example.service.*)")
  • args() – matches on argument types.
    @Before("args(String, int)")
  • @annotation() – matches methods with a specific annotation.
    @Before("@annotation(org.springframework.transaction.annotation.Transactional)")
  • bean() – matches specific Spring bean name.
    @Before("bean(myService)")

You can combine expressions with &&, ||, ! :


@Before("execution(* com.example.service.*.*(..)) && args(name,age)")
public void adviceWithArgs(String name, int age) {
System.out.println("Name: " + name + ", Age: " + age);
}
Two Minute Drill
  • Pointcut expression selects which joinpoints to apply advice.
  • Most common is execution() with pattern matching.
  • Use within() for type matching, args() for parameters.
  • @annotation() matches methods with specific annotations.
  • Combine expressions with &&, ||, ! for precise control.

Need more clarification?

Drop us an email at career@quipoinfotech.com