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.
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><bean id="settersAndAbsquatulatePointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>.*set.*</value>
<value>.*absquatulate</value>
</list>
</property>
</bean>
</code>RegexpMethodPointcutAdvisor combines a pointcut and an advice.
<code><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>
</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
StaticMethodMatcherPointcutfor creating custom pointcuts. Subclass it and implement the abstract
matchesmethod.
<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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.