Success with AOP in Grails 1.1

Post by Scott Vlaminck

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.

  1. 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).
  2. The second set of changes I made were in the aop bean definition in resources.groovy.
    1. First, I needed to make sure that I was proxying the target class (proxy-target-class=true).
    2. 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').

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….

About Scott Vlaminck

believes software development is more about people than technology; believes in agile processes; software developer, engineer, designer, architect, or whatever they're calling us these days; enjoys discussing software design; working on a program to write other programs (but it hasn't written itself yet).
This entry was posted in Agile Processes. Bookmark the permalink.

You may also like:

2 Responses to Success with AOP in Grails 1.1

  1. Pingback: refactr blog on software development, design, agile processes, and business Blog Archive » CircuitBreaker Pattern in Grails

  2. Pingback: refactr blog on software development, design, agile processes, and business Blog Archive » Updates to Circuit Breaker Grails Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>