Java Reference
In-Depth Information
The
@Pointcut
annotation is how you create a
named
pointcut. The name is set by the
name of the method on which it's applied. The three pointcuts here match the
deposit
,
withdraw
, and
getBalance
methods in the
Account
class. The
@Before
advice
combines them using an
or
expression and logs the method calls. When running the
Ac-
countSpec
tests, the (truncated) output is similar to this:
Jun 28, 2013 12:03:29 PM
INFO: getBalance called with [] on mjg.spring.entities.Account(id:4,
balance:100.0)
Jun 28, 2013 12:03:29 PM
INFO: deposit called with [100] on mjg.spring.entities.Account(id:8,
balance:100.0)
INFO: withdraw called with [100] on mjg.spring.entities.Account(id:9,
balance:100.0)
Jun 28, 2013 12:03:29 PM
INFO: getBalance called with [] on mjg.spring.entities.Account(id:9,
balance:0.0)
The
JoinPoint
can be used to get more information, but those are AOP details rather
than Groovy.
In both of these examples the aspect was provided in its own class. Spring provides an al-
ternative, however, in the form of beans defined right in the bean definition file.
7.4. Inline scripted beans
Another capability Spring provides to beans from dynamic languages is that they can be
12
I have to admit that in several years of using Spring and Groovy I've never found a compelling use case for inline
scripted beans that couldn't have been handled with regular classes. If you have one, please let me know.
Here's an example. The following sections can be used in a bean configuration file, as
shown in the next listing.
Listing 7.20. Additions to bean configuration file for an inline scripted aspect
<lang:groovy id=
"aspectScript"
>
<lang:inline-script>
<![CDATA[