major reason is that JDK dynamic proxy only supports proxying of interfaces. We will discuss proxies in
detail in the section "Understanding Proxies."
Joinpoints in Spring
One of the more noticeable simplifications in Spring AOP is that it supports only one joinpoint type:
Method Invocation. At first glance, this might seem like a severe limitation if you are familiar with other
AOP implementations like AspectJ, which supports many more joinpoints, but in fact this actually makes
Spring more accessible.
The Method Invocation joinpoint is by far the most useful joinpoint available, and using it, you can
achieve many of the tasks that make AOP useful in day-to-day programming. Remember that if you
need to advise some code at a joinpoint other than a Method Invocation, you can always use Spring and
AspectJ together.
Aspects in Spring
In Spring AOP, an aspect is represented by an instance of a class that implements the Advisor interface.
Spring provides a selection of convenience Advisor implementations that you can use in your
applications, thus removing the need for you to create lots of different Advisor implementations for your
example. There are two subinterfaces of Advisor: IntroductionAdvisor and PointcutAdvisor. The
PointcutAdvisor interface is implemented by all Advisors that use pointcuts to control the applicability of
advice to joinpoints.
In Spring, introductions are treated as special kinds of advice. Using the IntroductionAdvisor
interface, you can control those classes to which an introduction applies. We cover this in more detail in
the next chapter.
We discuss the different PointcutAdvisor implementations in detail later in this chapter in the
section "Advisors and Pointcuts in Spring."
About the ProxyFactory Class
The ProxyFactory class controls the weaving and proxy creation process in Spring AOP. Before you
can actually create a proxy, you must specify the advised or target object. You can do this, as you saw
earlier, using the setTarget() method. Internally, ProxyFactory delegates the proxy creation process
to an instance of DefaultAopProxyFactory, which in turn delegates to either Cglib2AopProxy or
JdkDynamicAopProxy, depending on the settings of your application. We discuss proxy creation in more
detail later in this chapter.
Using the ProxyFactory class, you control which aspects you want to weave into the proxy. As
mentioned earlier, you can weave only an aspect--that is, advice combined with a pointcut--into
advised code. However, in some cases you want an advice to apply to the invocation of all methods
in a class, not just a selection. For this reason, the ProxyFactory class provides the addAdvice() method
that you saw in Listing 6-3. Internally, addAdvice() wraps the advice you pass it in an instance of
DefaultPointcutAdvisor, which is the standard implementation of PointcutAdvisor, and configures it
with a pointcut that includes all methods by default. When you want more control over the Advisor that
is created, or when you want to add an introduction to the proxy, create the Advisor yourself and use
the addAdvisor() method of the ProxyFactory().
You can use the same ProxyFactory instance to create many different proxies, each with different
aspects. To help with this, ProxyFactory has removeAdvice() and removeAdvisor() methods, which allow
you to remove any advice or Advisors from the ProxyFactory that you previously passed to it. To check
whether a ProxyFactory has a particular advice attached to it, call adviceIncluded(), passing in the
advice object for which you want to check.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home