requirements. Remember that if you can't find a pointcut to suit your needs, you can create your own
implementation from scratch by implementing Pointcut, MethodMatcher, and ClassFilter.
You use two patterns to combine pointcuts and advisors. The first pattern, the one we have used
so far, involves having the pointcut implementation decoupled from the advisor. In the code we have
seen up to this point, we created instances of Pointcut implementations and then used the
DefaultPointcutAdvisor to add advice along with the Pointcut to the proxy.
The second option, one that is adopted by many of the examples in the Spring documentation, is
to encapsulate the Pointcut inside your own Advisor implementation. This way, you have a class that
implements both Pointcut and PointcutAdvisor, with the PointcutAdvisor.getPointcut() method
simply returning this. This is an approach many classes, such as StaticMethodMatcherPointcutAdvisor,
use in Spring.
We find that the first approach is the most flexible, allowing you to use different Pointcut
implementations with different Advisor implementations. However, the second approach is useful in
situations where you are going to be using the same combination of Pointcut and Advisor in different
parts of your application, or, indeed, across many different applications. The second approach is useful
when each Advisor must have a separate instance of a Pointcut; by making the Advisor responsible for
creating the Pointcut, you can ensure that this is the case.
If you recall the discussion on proxy performance from the previous chapter, you will remember
that unadvised methods perform much better than methods that are advised. For this reason, you
should ensure that, by using Pointcuts, you only advise the methods that are absolutely necessary. This
way, you reduce the amount of unnecessary overhead added to your application by using AOP.
Getting Started with Introductions
Introductions are an important part of the AOP feature set available in Spring. By using introductions,
you can introduce new functionality to an existing object dynamically. In Spring, you can introduce an
implementation of any interface to an existing object. You may well be wondering exactly why this is
useful--why would you want to add functionality dynamically at runtime when you can simply add that
functionality at development time? The answer to this question is easy. You add functionality
dynamically when the functionality is crosscutting and is not easily implemented using traditional
advice.
Introduction Basics
Spring treats introductions as a special type of advice, more specifically, as a special type of around
advice. Because introductions apply solely at the class level, you cannot use pointcuts with
introductions; semantically, the two don't match. An introduction adds new interface implementations
to a class, and a pointcut defines which methods an advice applies. You create an introduction by
implementing the IntroductionInterceptor interface, which extends the MethodInterceptor and the
DynamicIntroductionAdvice interfaces. Figure 7-2 shows this structure along with the methods of both
interfaces.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home