About Roger Hughes

Auditing a Spring MVC Webapp with AspectJ. Part 1

If you’re like me, then you’ll have those kinds of programming days where everything seems to go incredibly well. You write the code and the tests and it just works. And then and there are those other kinds of days, the really bad ones, where you know that everything you’ve written is as right as it can be and the code refuses to work: something is obviously wrong, but you’ve no idea what. I had one of these kinds of days when writing the code for this blog. The idea was to demonstrate how to use Spring and Aspectj to audit a user’s visits to a screen.

Auditing a user’s visits to a screen is one of those few cross-cutting concerns that Aspect Oriented Programming (AOP) solves very well. The idea in the case of my demo code, is that you add an annotation to the appropriate controllers and every time a user visits a page, then that visit is recorded. Using this technique you can construct a picture of the most popular screens and therefore the most popular chunks of functionality in your application. Knowing these details makes it easier to decide where to aim your development effort as it doesn’t pay to develop those chunks of your application that hardly anyone ever uses.

I’ve talked about AspectJ and AOP before in the following blogs, which is great as they demonstrate the basics, but they aren’t a real working Spring MVC application. My previous blogs were:

This time I thought that I’d come up will a fully functioning Spring MVC application using a useful AOP cross-cutting concern.

For the demo-code I created a simple Spring MVC application that has two screens: a home page and a help page. On top of this I’ve created a simple annotation: @Audit, which is used to mark a controller as one that needs auditing (not all of them will, especially if you choose to audit function points rather than individual screens) and to tell the advice object the screen id as demonstrated in the snippet of code below:

The plan of attack was to write my simple @Audit annotation and handle it using a simple AuditAdvice class with a method annotated with Aspectj’s @Before annotation. I would then pretend this was a real advice class, which meant delegating the actual auditing to an autowired AuditService object.

I started by creating a sample Spring MVC application using the Spring project template:

I then put all the code together and expected it to just work, except it didn’t: Spring would not, no matter what I tried, autowire the AuditService into the AuditAdvice class. This meant that when my @Before annotated method was called, it threw a NullPointerException

When you’re in the situation where you strongly suspect your code is right and it just won’t work, then one of the areas for investigation is the project POM file and setup.

The thing is, when you use someone else’s API, project setup or other tool, you tend to trust it more than you would trust your own code. I guess that the reasons for this are that it’s usually written by a highly respectable organization, which makes you kind of think that they have some magical way of writing really good code, plus it usually contains a whole bunch of stuff that you don’t really understand.

This is really irrational as the API, tool, configuration file etc have been written by programmers like you and me, who probably make as many mistakes as we do.

This fact usually worries me every time I step on to an airplane, as a software bug at 30,000ft is not something you want to happen.

The problem with the Spring MVC project POM is that it’s slightly obsolete and full of stuff that you just don’t need for a standard Spring MVC Java application, plus, there’s no links to any of the plugin documentation, so finding out what it all does and what the different settings mean is difficult.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Recent Jobs

No job listings found.

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.