img
As you can see, calling writeMessage() on the untouched target object resulted in a standard Method
Invocation, and no extra content is written to console output. However, the invocation of the proxy caused
the code in the MessageDecorator to execute, creating the desired output of "Hello World!" From this
example, you can see that the advised class had no dependencies on Spring or the AOP Alliance interfaces;
the beauty of Spring AOP, and indeed AOP in general, is that you can advise almost any class, even if that
class was created without AOP in mind. The only restriction, in Spring AOP at least, is that you can't advise
final classes, because they cannot be overridden and therefore cannot be proxied.
Spring AOP Architecture
The core architecture of Spring AOP is based around proxies. When you want to create an advised
instance of a class, you must use the ProxyFactory class to create a proxy of an instance of that class,
first providing the ProxyFactory with all the aspects that you want to be woven into the proxy. Using
ProxyFactory is a purely programmatic approach to creating AOP proxies. For the most part, you don't
need to use this in your application; instead, you can rely on the declarative AOP configuration
mechanisms provided by Spring (the ProxyFactoryBean class, the aop namespace, and @AspectJ-style
annotations) to provide declarative proxy creation. However, it is important to understand how proxy
creation works. For the rest of this chapter, we will use the programmatic approach to proxy creation. In
the next chapter, we discuss using Spring's declarative AOP configurations.
At runtime, Spring will analyze the cross-cutting concerns defined for the beans in the
ApplicationContext and generate proxy beans (which wraps the underlying target bean) dynamically.
Instead of calling the target bean directly, callers are injected with the proxied bean, and any calls to the
target are received by the proxy bean. The proxy bean will then analyze the running condition (i.e.,
joinpoint, pointcut, advice, etc.) and weave in the appropriate advice accordingly. Figure 6-1 shows a
high-level view of a Spring AOP proxy in action.
Figure 6-1. Spring AOP proxy in action
Internally, Spring has two proxy implementations: the JDK dynamic proxy and the CGLIB proxy. By
default, when the target object to be advised implements some sort of an interface, Spring will use a JDK
dynamic proxy to create proxy instances of the target. However, when the advised target object doesn't
implement an interface (e.g., it's a concrete class), CGLIB will be used for proxy instance creation. One
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home