Success with AOP in Grails 1.1
Success! After playing around with a few things, I got AOP working in Grails this weekend and I’ve checked in the changes to my example app. I’ve also added a test that more clearly shows aop in action.
I made a few changes inspired by Graeme’s comment on my earlier post on AOP in Grails. Graeme pointed me to BeanBuilderTests and both testSpringAOPSupport() and testUseSpringNamespaceAsMethod() were very helpful.
I also took a deeper look at the Spring docs on AOP – specifically around advice and advice params.
Looking at the changes I made, the first change clearly make some sense in retrospect. The second is a little confusing to me and I’m guessing that I’m doing something wrong.
- In my Aspects, I switched from using the aopalliance classes to aspectj classes. This meant instead of using org.aopalliance.intercept.MethodInvocation & MethodInterceptor classes, I used to org.aspectj.lang.ProceedingJoinPoint. That makes sense and I was just wrong before (not sure where I saw examples of this).
- The second set of changes I made were in the aop bean definition in resources.groovy.
- First, I needed to make sure that I was proxying the target class (
proxy-target-class=true). - Second, I had to define my execution point without explicit parameters. As in
Service.method(..)vs.Service.method(Integer) && args(i)(which I used with'arg-names':'i').
- First, I needed to make sure that I was proxying the target class (
The last changes are what confuse me. I tried a number of different things, but could not get explicit parameters to work. Apparently, I need read through the Spring docs on advice params again.
One new wrinkle is that when running the app, my around advice seems to be invoked twice. I wrote the test (inspired by BeanBuilderTests), but I don’t see the same problem when executing the test. Maybe it still has something to do with the pointcut execution declaration. My quest continues….
This entry was posted by Scott Vlaminck on Monday, June 1st, 2009 at 9:17 am and is filed under Agile Processes. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
[...] the next generation of software for people who expect more from their web applications. « Success with AOP in Grails 1.1 [...] ...on June 10th, 2009 at 9:36 am
[...] Tuesday night’s GUM meeting, I presented my experiences with AOP in Grails and we discussed the Circuit Breaker Plugin for most of the meeting. As part of that discussion, I [...] ...on June 11th, 2009 at 10:28 pm