In this tutorial we will create, from the very beginning, a simple application that uses Spring and its Aspect Oriented Programming capabilities. We will use schema-based approach from Spring 2.0 to define the AOP configuration.
I am not going to cover here the AOP or Spring basis, if you are interested what AOP is or why would you use it, try documentation on Spring or AspectJ website.
Let’s create a very simple class – we will base all the examples on it:
We also need a main class that we’ll be running – again, very simple one will be enough:
Our spring.xml contains a definition of person bean:
Aspect Oriented Programming with Spring
It’s all nice & easy so far – but let’s add AOP to that mix. We basically need two things:
- We need the functionality/feature/concern that needs to run across different classes – using proper terminology we need our aspect.
- We need to tell (advice) Spring when this aspect should run. The first bit is very simple – aspect can simply be a class with a method:
The second bit will be defined declaratively in our spring.xml. First we need our “aspect class” as a normal bean:
Then we declare AOP behavior – all is wrapped in <aop:config> tag (I’ll cover aop namespace shortly). We need to refer to our inject bean:
Then we need to say when it should run. The when in Spring is simple – it always refers to some method execution. In our example, we would like to run our InjectMe class when any method from Person is executed – in AOP terminology it means we need to define a pointcut that refers to all those methods:
The pointcut simply points to the methods, we need to specify when exactly aspect should run (before the method executes or maybe after?). We also need to say which method of our inject bean is to be executed:
That’s it! When we run the example, the output will look like below:
About to print a name... INJECT! My name: Tomasz Muras About to print a website... INJECT! My website: http://techblog.zabuchy.net Private method Public method About to throw an exception... INJECT!
Notice that the aspect was run 3 times – and we have run 3 public methods on an object of Person class. When object executes its own method, AOP did not work. To complete Spring’s XML file, we need to add aop namespace and schema. The full spring.xml:
Run after returning from a method
If we would like to execute the aspect after the method is run, we replace
Updated XML file:
And the output this time. Note that the aspect did not execute after a method has thrown an exception.
About to print a name... My name: Tomasz Muras INJECT AFTER! Returned: null About to print a website... My website: http://techblog.zabuchy.net Private method Public method INJECT AFTER! Returned: null About to throw an exception...
Run after an exception is thrown and run always (finally)
You can also run aspect only after a method throws an exception with <aop:after-throwing> element or in both cases using <aop:after>. You will find the examples of both in the attached source code.
Run before and after the exception
The most powerful advice is <aop:around>. It runs both before and after method execution and it gives you the opportunity to cancel method execution altogether.
The schema definition looks just like for previous advices:
Advice method must have the first argument of type org.aspectj.lang.ProceedingJoinPoint. When/if we decide to run the actual method, we run proceed() method on the ProceedingJoinPoint object.
Source code and dependencies
You can download full source code with all the examples above. Here is a list of jars I’ve used for compiling the example: