img
Figure 7-1. UML sequence diagram for a control flow pointcut
Using Composable Pointcut
In previous pointcutting examples, we used just a single pointcut for each Advisor. In most cases, this is
usually enough, but in some cases, you may need to compose two or more pointcuts together to achieve
the desired goal. Consider the situation where you want to pointcut all getter and setter methods on a
bean. You have a pointcut for getters and a pointcut for setters, but you don't have one for both. Of
course, you could just create another pointcut with the new logic, but a better approach is to combine
the two pointcuts into a single pointcut using ComposablePointcut.
The ComposablePointcut supports two methods: union() and intersection(). By default,
ComposablePointcut is created with a ClassFilter that matches all classes and a MethodMatcher that
matches all methods, although you can supply your own initial ClassFilter and MethodMatcher during
construction. The union() and intersection() methods are both overloaded to accept ClassFilter and
MethodMatcher arguments.
The ComposablePointcut.union() method can be called by passing in an instance of either the
ClassFilter, MethodMatcher, or Pointcut interface. The result of an union operation is that the
ComposablePointcut will add an "or" condition into its call chain for matching with the joinpoints. It's
the same for the ComposablePointcut.intersection() method, but this time an "and" condition will be
added instead, which means that all ClassFilter, MethodMatcher, and Pointcut definitions within the
ComposablePointcut should be match for applying an advice. You can imagine it as the "where" clause in
a SQL query, with the union() method like the "or" operator and the intersection() method like the
"and" operator.
As with control flow pointcuts, this is quite difficult to visualize, and it is much easier to understand
with an example. Listing 7-4 shows a simple bean with three methods.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home