Spring seems to be the tool of choice for many new web projects nowadays, and while there’s a wealth of examples and documentation out there, one particular use case eluded me quite a bit: AOP with annotations. I don’t like xml, I prefer annotations and convections, so here’s a step by step example on how to get a basic example up and running. In this very basic example we’ll create a logging aspect and wrap it around an object.
First let’s get everything setup. We’ll start with an empty webproject, and we’ll add the following libraries:
All of the jars from the spring-framework-3.0.1 zip. (from SpringSource)
jstl-api-1.2.jar and jstl-impl-1.2.jar(from Java site)
Maybe not required, but always useful.
Now let’s create a basic web.xml:
The xml simply initializes the spring framework, by passing the location of the main configuration file ( /WEB-INF/spring/web-application-context.xml ). We also map our spring actions to *.do. Now let’s look at the web-application-context.xml file:
This file defines which package should be scanned for components (test.spring.actions in this case) and imports two further files. One of these files controls the AOP part of the framework, while the other controls the MVC (Model-View-Controller) part of the framework. Let’s look at the aop-context.xml file:
Of note here, is the aspectj-autoproxy tag, which is required to enable the creation of proxy object to apply the aspects. Now, lets look at the code. The first class is the logger aspect that we will be applying to one or more classes:
Here we’re using the @Around annotation to declare an around join point, since this type gives us more information regarding the class and method we’re wrapping. In the same annotation, we also specify that this point cut should be applied to all methods of the LogObject class. This is the point cut expression, and it’s fairly flexible. More information can be found elsewhere in the web, including the Spring documentation. Other than this, our aspect just logs the entry and exit of the methods.
The actual class we’re applying the aspect to is rather uninteresting:
Of course this is where your actual application logic would live, but the purpose here is to provide the simplest example possible. The only notable aspect of this class is that it implements an interface. In our controller we’ll use the interface instead of the actual implementation, since Spring will wire a dynamic proxy of the real object. In any case, it is normally good practice to program against interfaces.
Last is our controller, where we tie everything together:
The controller is letting Spring autowire the ILogObject, which will be proxied to provide the functionality provided by our logging aspect. So, go ahead and hit the action from your browser, in the console you will see the log messages before and after we execute the view() method.
So this is it, I hope this helps people as a basic starting point for using AOP inside spring, without having to write too much xml.