img
Listing 7-25. Configuring Spring AOP with the aop Namespace (Around Advice)
...
<aop:aspect ref="advice">
<aop:before pointcut-ref="fooExecution"
method="simpleBeforeAdvice"/>
<aop:around pointcut-ref="fooExecution"
method="simpleAroundAdvice"/>
</aop:aspect>
...
We just added a new tag <aop:around> to declare the around advice and reference the same
pointcut. Run the testing program again, and you will have the following output:
Before execution: com.apress.prospring3.ch7.aopns.MyDependency foo argument: 100
foo(int): 100
After execution: com.apress.prospring3.ch7.aopns.MyDependency foo argument: 100
Executing: com.apress.prospring3.ch7.aopns.MyDependency foo argument: 101
Before execution: com.apress.prospring3.ch7.aopns.MyDependency foo argument: 101
foo(int): 101
After execution: com.apress.prospring3.ch7.aopns.MyDependency foo argument: 101
bar()
There are two interesting points here. First, you see that the around advice was applied to both
invocations of the foo() method, since it doesn't check the argument. Second, for the foo() method with
101 as an argument, both the before and around advice were executed, and by default the before advice
takes precedence.
Note When using the aop namespace or the @AspectJ style, there are two types of after advice. The "after-
returning" advice (using the <aop:after-returning> tag) applies only when the target method is completed
normally. Another one is the "after" advice (using the <aop:after> tag), which takes place whether the method
was completed normally or the method runs into an error and an exception is thrown. If you need an advice that
executes regardless of the execution result of the target method, you should use the after advice.
Using @AspectJ-Style Annotations
As you might expect, when using Spring AOP with JDK 5 or above, you can also use annotation to
declare your advice. Spring supports the @AspectJ style annotations that make use of the annotations
and syntax provided by @AspectJ. However, as stated before, Spring still uses its own proxying
mechanism for advising the target methods, not AspectJ's weaving mechanism.
In this section, we will go through how to implement the same aspects like the one in aop-namespace,
by using @AspectJ style annotations. For the examples in this section, we will also use annotation for other
Spring beans as well.
Listings 7-26 and 7-27 shows the MyDependency and MyBean classes with Spring's DI annotations.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home