img
In Listing 7-3, the advised proxy is assembled with ControlFlowPointcut and then the foo() method
is invoked twice: once directly from the run() method and once from the test() method. Here is the line
of particular interest:
Pointcut pc = new ControlFlowPointcut(ControlFlowExample.class, "test");
In this line, we are creating a ControlFlowPointcut instance for the test() method of the
ControlFlowExample class. Essentially, this says, "Pointcut all methods that are called from the
ControlFlowExample.test() method." Note that although we said "Pointcut all methods," in fact, this
really means "Pointcut all methods on the proxy object that is advised using the Advisor corresponding
to this instance of ControlFlowPointcut."
You also need to add the dependency for CGLIB into your project, which was shown in Table 7-2.
Table 7-2. Dependency for CGLIB
Group ID
Artifact ID
Version
Description
2.2.2
Code generation library required by Spring AOP
cglib
cglib
Running the example in Listing 7-3 yields the following output:
Trying normal invoke
foo()
Trying under ControlFlowExample.test()
Before method: public void com.apress.prospring3.ch7.cflow.TestBean.foo()
foo()
As you can see, when the foo() method is first invoked outside of the control flow of the test()
method, it is unadvised. When it executes for a second time, this time inside the control flow of the
test() method, the ControlFlowPointcut indicates that its associated advice applies to the method, and
thus the method is advised. Note that if we had called another method from within the test() method,
one that was not on the advised proxy, it would not have been advised.
Control flow pointcuts can be extremely useful, allowing you to advise an object selectively only
when it is executed in the context of another. However, be aware that you take a substantial
performance hit for using control flow pointcut over other pointcuts. Figures from the Spring
documentation indicate that a control flow pointcut is typically five times slower than other pointcuts
on a 1.4 JVM.
Let's consider an example. Suppose we have a transaction processing system, which contains a
TransactionService interface as well as an AccountService interface. We would like to apply an
after advice so that when the AccountService.updateBalance() method is called by
TransactionService.reverseTransaction(), an e-mail notification is sent to the customer, after the
account balance is updated. However, e-mail will not be sent under any other circumstances. In this
case, the control flow pointcut will be useful. Figure 7-1 shows the UML sequence diagram for this
scenario.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home