The SetterMethodMatcher matches all methods that start with set, and it is combined with the
ComposablePointcut using union() for the second round of invocations. At this point, we have a union of
two MethodMatchers: one that matches all methods starting with get and one that matches all methods
starting with set. To this end, we expect that all invocations during the second round will be advised.
The GetAgeMethodMatcher is very specific and matches only the getAge() method. This MethodMatcher
is combined with the ComposablePointcut using intersection() for the third round for invocations.
Because the GetAgeMethodMatcher is being composed using intersection(), the only method that we
expect to be advised in the third round of invocations is getAge(), because this is the only method that
matches all the composed MethodMatchers.
Running this example results in the following output:
Test 1
Before method: public int com.apress.prospring3.ch7.composable.SampleBean.getAge()
Before method: public java.lang.String com.apress.prospring3.ch7
.composable.SampleBean.getName()
Test 2
Before method: public int com.apress.prospring3.ch7.composable.SampleBean.getAge()
Before method: public java.lang.String com.apress.prospring3.ch7
.composable.SampleBean.getName()
Before method: public void com.apress.prospring3.ch7
.composable.SampleBean.setName(java.lang.String)
Test 3
Before method: public int com.apress.prospring3.ch7.composable.SampleBean.getAge()
As expected, the first round of invocations on the proxy saw only the getAge() and getName()
methods being advised. For the second round, when the SetterMethodMatcher had been composed with
the union() method, all methods were advised. In the final round, as a result of the intersection of the
GetAgeMethodMatcher, only the getAge() method was advised.
Although this example demonstrated the use of MethodMatchers only in the composition process, it
is just as simple to use ClassFilter when you are building the pointcut. Indeed, you can use a
combination of MethodMatchers and ClassFilters when building your composite pointcut.
Composition and the Pointcut Interface
In the previous section, you saw how to create a composite pointcut using multiple MethodMatchers and
ClassFilters. You can also create composite pointcuts using other objects that implement the Pointcut
interface.
Another way for constructing a composite pointcut is to use the
org.springframework.aop.support.Pointcuts class. The class provides three static methods. The
intersection() and union() methods both take two pointcuts as arguments to construct a composite
pointcut. On the other hand, a matches(Pointcut, Method, Class, Object[]) method also is provided
for performing a quick check on whether a pointcut matches with the provided method, class, and
method arguments.
The Pointcuts class supports operations on only two pointcuts. So, if you need to combine
MethodMatcher and ClassFilter with Pointcut, you need to use the ComposablePointcut class. However,
when you just need to combine two pointcuts, the Pointcuts class will be more convenient.
Pointcutting Summary
From the discussions in this chapter and in the previous chapter, you can see that Spring offers a
powerful set of Pointcut implementations that should meet most, if not all, of your application's
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home