« Maven AspectJ 3.0 plugin released | Main | What the teacher said (using AspectJ with Spring part II) »

May 18, 2004

Using AspectJ with Spring - part 1

This is the first of what I envisage will be several entries exploring how AspectJ and Spring can be used together. Today I'm just going to introduce a very simple example that I can build on in later posts, and that shows an aspect working seamlessly (if unsurprisingly) in a Spring application. The example takes its inspiration directly from Aslak's pico container talk at TSS.

From a Spring perspective (the AspectJ pieces of this example could equally well be used with pico, nano, or HiveMind), the example hinges around a Girl kissing a Boy, and the question of how our Girl finds her prince charming.

Some things in life can be kissed:

public interface Kissable { void kiss(); }

Girls, now they'll kiss anything...

public class Girl { private Kissable k; public void setKissable(Kissable toKiss) { this.k = toKiss; } public void kiss() { k.kiss(); } }

Boys are just one of the many things that can be kissed:

public class Boy implements Kissable { public void kiss() { System.out.println("wahay!"); } }

We can wire the application up with Spring to create a Girl bean and a Boy bean, and help them get it together.

<beans> <bean id="girl" class="Girl"> <property name="kissable"><ref bean="boy"/></property> </bean> <bean id="boy" class="Boy"> </bean> </beans>

Finally we need a minimal "main" to fire up the container and run the application:

public class Main { public static void main(String[] args) throws Exception { XmlBeanFactory beanFactory = new XmlBeanFactory(new FileInputStream("beans.xml")); Girl g = (Girl) beanFactory.getBean("girl"); g.kiss(); } }

If you run this stunning program, you get the output


as the girl kisses the boy. So far, not an aspect in site.

Finally, it's time to add an aspect into the mix. Let's move the scene to a school playground and see what happens when a Teacher observes a girl and boy kissing:

public aspect Teacher { /** * There are many things that can be kissed, and many possible kissers, * but here we're just interested in Girls kissing Boys. */ pointcut aGirlKissingABoy() : call(* Kissable.kiss()) && this(Girl) && target(Boy); /** * Now we get to decide what the teacher's reaction is to the kiss... */ after() returning : aGirlKissingABoy() { System.out.println("Let's keep that behaviour for after school hours shall we?"); } }

Run the program again, and now you'll see the output:

Let's keep that behaviour for after school hours shall we?

In tomorrow's installment, I'll get into the issue of how we could use Spring to make the Teacher's response a configurable property on a "teacher" bean (i.e. how to use Spring to configure aspects).

I think the Teacher aspect is a very clear and direct implementation. Given a system with several different Kissable classes, and several different clients that call them, how would you code this in straight OO? Do you have a solution that is non-intrusive on the Girl and Boy classes (after all, they really don't care what the teacher thinks)? What if I tell you that my next requirement will be for the teacher to react when a Girl kisses a Frog too?

Posted by adrian at May 18, 2004 02:25 PM [permalink]


This is good stuff. I'm a huge Spring fanatic and a closet AspectJ fan. Even though Spring has awesome AOP support, it seems to be focused on solving enterprise-level problems such as declarative transactions and has its limits when standing up next to AspectJ. I've always thought that AspectJ could be used alongside Spring to fill in the missing pieces of Spring's own AOP framework, but I've never taken the time to ponder when or where AspectJ is more appropriate than Spring's AOP. Maybe as you build upon this series of blog entries you could discuss the pros and cons of using AspectJ with Spring vs. Spring's own AOP.

Posted by: Craig Walls at May 18, 2004 08:52 PM

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Remember me?