Backend Development 9 min read

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.

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

RegexpMethodPointcutAdvisor combines a pointcut and an advice.

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

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

.

<code>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;
  }
}
</code>

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

<code>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(); }
}
</code>

3 Pointcut Superclass

Spring provides abstract pointcut super‑classes such as

StaticMethodMatcherPointcut

for creating custom pointcuts. Subclass it and implement the abstract

matches

method.

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

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

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

login 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.