As you can see from this code, the Pointcut interface defines two methods, getClassFilter() and
getMethodMatcher(), which return instances of ClassFilter and MethodMatcher, respectively. When
creating your own pointcuts from scratch, you must implement both the ClassFilter and MethodMatcher
interfaces as well. Thankfully, as you will see in the next section, this is usually unnecessary because
Spring provides a selection of Pointcut implementations that cover almost if not all of your use cases.
When determining whether a Pointcut applies to a particular method, Spring first checks to see
whether the Pointcut applies to the method's class using the ClassFilter instance returned by
Pointcut.getClassFilter(). Listing 6-20 shows the ClassFilter interface.
Listing 6-20. The ClassFilter Interface
org.springframework.aop;
public interface ClassFilter {
boolean matches(Class<?> clazz);
}
As you can see, the ClassFilter interface defines a single method, matches(), that is passed an
instance of Class that represents the class to be checked. As you have no doubt determined, the
matches() method returns true if the pointcut applies to the class and false otherwise.
The MethodMatcher interface is more complex than the ClassFilter interface, as shown in Listing
6-21.
Listing 6-21. The MethodMatcher Interface
package org.springframework.aop;
public interface MethodMatcher {
boolean matches(Method m, Class<?> targetClass);
boolean isRuntime();
boolean matches(Method m, Class<?> targetClass, Object[] args);
}
Spring supports two different types of MethodMatcher, static and dynamic, determined by the
return value of isRuntime(). Before using a MethodMatcher, Spring calls isRuntime() to determine
whether the MethodMatcher is static, indicated by a return value of false, or dynamic, indicated by a
return value of true.
For a static pointcut, Spring calls the matches(Method, Class<T>) method of the MethodMatcher once
for every method on the target, caching the return value for subsequent invocations of those methods. In
this way, the check for method applicability is performed only once for each method, and subsequent
invocations of a method do not result in an invocation of matches().
With dynamic pointcuts, Spring still performs a static check using matches(Method, Class<T>) the
first time a method is invoked to determine the overall applicability of a method. However, in addition to
this and provided that the static check returned true, Spring performs a further check for each
invocation of a method using the matches(Method, Class<T>, Object[]) method. In this way, a dynamic
MethodMatcher can determine whether a pointcut should apply based on a particular invocation of a
method, not just on the method itself. For example, a pointcut needs to be applied only when the
argument is an Integer with a value larger than 100. In this case, the matches(Method, Class<T>,
Object[]) method can be coded to perform further checking on the argument for each invocation.
Clearly, static pointcuts--that is, pointcuts whose MethodMatcher is static--perform much better
than dynamic pointcuts because they avoid the need for an additional check per invocation. That said,
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home