Using @AspectJ-style annotations: Besides the XML-based aop namespace, you
·
can also use the @AspectJ-style annotations within your classes for configuring
Spring AOP. Although the syntax it uses is based on AspectJ and you need to
include some AspectJ libraries when using this option, Spring still uses the proxy
mechanism, i.e., creates proxied objects for the targets, when bootstrapping the
ApplicationContext.
Let's go through the option one by one in the following sections.
Using ProxyFactoryBean
The ProxyFactoryBean class is an implementation of FactoryBean that allows you to specify a bean to
target, and it provides a set of advice and advisors for that bean that are eventually merged into an AOP
proxy. Because you can use both advisor and advice with the ProxyFactoryBean, you can configure not
only the advice declaratively but the pointcuts as well.
ProxyFactoryBean shares a common interface (the org.springframework.aop.framework.Advised
interface) with ProxyFactory (both classes extend the org.springframework.aop.framework
.AdvisedSupport class indirectly, which implements the Advised interface), and as a result, it exposes
many of the same flags such as frozen, optimize, and exposeProxy. The values for these flags are passed
directly to the underlying ProxyFactory, which allows you to configure the factory declaratively as well.
ProxyFactoryBean in Action
Using ProxyFactoryBean is actually very simple. You define a bean that will be the target bean, and then
using ProxyFactoryBean, you define the bean that your application will actually access, using the target
bean as the proxy target. Where possible, define the target bean as an anonymous bean inside the proxy
bean declaration. This prevents your application from accidentally accessing the unadvised bean.
However, in some cases, such as the sample we are about to show you, you may want to create more
than one proxy for the same bean, so you should use a normal top-level bean for this case.
Listings 7-11 and 7-12 show two classes, one of which has a dependency on the other.
Listing 7-11. The MyDependency Class
package com.apress.prospring3.ch7.pfb;
public class MyDependency {
public void foo() {
System.out.println("foo()");
}
public void bar() {
System.out.println("bar()");
}
}
Listing 7-12. The MyBean Class
package com.apress.prospring3.ch7.pfb;
public class MyBean {
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home