Mastering Spring AOP Pointcuts: Static, Dynamic, and Custom Implementations

This article explains Spring's pointcut implementations, covering static and dynamic pointcuts, regular‑expression based pointcuts, the RegexpMethodPointcutAdvisor, control‑flow pointcuts, and how to create custom pointcuts by subclassing Spring’s abstract pointcut classes, with full XML and Java code examples.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring AOP Pointcuts: Static, Dynamic, and Custom Implementations

1 Static Pointcut

Static pointcuts are based on the method and target class and cannot consider method arguments. They are evaluated only once when the method is first called.

Regular‑expression pointcut (JdkRegexpMethodPointcut) matches methods whose signatures fit any of the provided patterns.

<bean id="settersAndAbsquatulatePointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
  <property name="patterns">
    <list>
      <value>.*set.*</value>
      <value>.*absquatulate</value>
    </list>
  </property>
</bean>

RegexpMethodPointcutAdvisor combines a pointcut and an advice.

<bean id="settersAndAbsquatulateAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  <property name="advice">
    <ref bean="beanNameOfAopAllianceInterceptor"/>
  </property>
  <property name="patterns">
    <list>
      <value>.*set.*</value>
      <value>.*absquatulate</value>
    </list>
  </property>
</bean>

2 Dynamic Pointcut

Dynamic pointcuts evaluate on each method call because they consider method arguments; they cannot be cached. The main example is the control‑flow pointcut, implemented by org.springframework.aop.support.ControlFlowPointcut.

public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher {
  private final Class<?> clazz;
  private final String methodName;
  @Override
  public boolean matches(Class<?> clazz) { return true; }
  @Override
  public boolean matches(Method method, Class<?> targetClass) { return true; }
  @Override
  public boolean isRuntime() { return true; }
  @Override
  public boolean matches(Method method, Class<?> targetClass, Object... args) {
    for (StackTraceElement element : new Throwable().getStackTrace()) {
      if (element.getClassName().equals(this.clazz.getName()) &&
          (this.methodName == null || element.getMethodName().equals(this.methodName))) {
        return true;
      }
    }
    return false;
  }
}

Example usage prints the method names from the current stack trace.

public class StackElementMain {
  public static void m1() { m2(); }
  public static void m2() {
    StackTraceElement[] elements = new RuntimeException().getStackTrace();
    for (StackTraceElement element : elements) {
      System.out.println(element.getMethodName());
    }
  }
  public static void main(String[] args) { m1(); }
}

3 Pointcut Superclass

Spring provides abstract pointcut super‑classes such as StaticMethodMatcherPointcut for creating custom pointcuts. Subclass it and implement the abstract matches method.

class TestStaticPointcut extends StaticMethodMatcherPointcut {
  @Override
  public boolean matches(Method m, Class targetClass) {
    // return true if custom criteria match
  }
}

Custom pointcuts can be arbitrarily complex, but using AspectJ pointcut expressions is recommended when possible.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaaopbackend-developmentspringPointcut
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.