Java tips, observations, bugs and problems from the world of Spring, Weblogic, Oracle, MySQL and many other technologies...

Tuesday, 2 July 2013

Auditing a Spring MVC Webapp with AspectJ. Part 2

Now, this is the blog you want to read if you're interested in creating a Spring MVC Webapp that uses Aspect Oriented Programming (AOP) in the form of Aspectj's @Aspect and @Before annotations to audit a user's visit to a screen.

As I said in my last blog 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.

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. This I've demonstrated in the snippet of code below:

Before getting stuck in to the AspectJ side of things, the first thing to do is to create a standard Spring MVC web app using the Spring Template designed for the job:

The next thing to do is to make a whole bunch of changes to the POM file as described in my previous blog. These are necessary for everything to work, though they're not all essential; however, be sure that you add the aspectJwearver dependency and remove the AspectJ plugin definition.

The app has two controllers and two simple JSPs. The first controller is the HomeController taken from the Spring MVC app and whilst the second is a HelpController designed to display help on any page of the application. I've included the HelpController's showHelp(…) method below, but that's just for completeness. It doesn't really matter in this case what the controllers do so long as there are a couple to audit.

The key things about this code are the retention policy and the target. The retention policy must be set to RetentionPolicy.RUNTIME, which means that the compiler doesn't throw the annotation away and makes sure that it's there, loaded into the JVM, at runtime. The @Target defines where you can apply the annotation. In this case I want it applied to methods only, so the target is ElementType.METHOD. The annotation MUST contain a value, which is this case is used to hold the name of the screen that the user is currently visiting.

The next key abstraction is the AuditAdvice class as shown below:

@Aspectpublic class AuditAdvice {

@Autowiredprivate AuditService auditService;

/**
* Advice for auditing a user's visit to a page. The rule is that the Before annotation
* applies to any method in any class in the com.captaindebug.audit.controller package
* where the class name ends in 'Controller' and the method is annotated by @Audit.
*
* @param auditAnnotation
* Audit annotation holds the name of the screen we're auditing.
*/@Before("execution(public String com.captaindebug.audit.controller.*Controller.*(..)) && @annotation(auditAnnotation) ")public void myBeforeLogger(Audit auditAnnotation) {

auditService.audit(auditAnnotation.value());}

}

This is annotated with two AspectJ annotations: @Aspect and @Before. The @Aspect annotation marks the AuditAdvice class as an aspect, whilst the @Before annotation means that the auditScreen(…) method gets called before any method whose definition matches the expression that is the @Before annotation's argument.

This expression is idea is rather cool. I've already covered the construction of the execution expression in my blog on Using AspectJ’s @AfterThrowing Advice in your Spring App; however, to sum this up, I'm going to apply the @Before annotated method to any method that has public visibility, returns a String, is in the com.captaindebug.audit.controller package and has the word Controller as part of the class name. In other words, I'm making it difficult to apply this execution expression to anything but my application's controllers and those controllers MUST be annotated by an @Audit annotation as described by the @annotation(auditAnnotation) expression and the auditScreen(…) method's Audit auditAnnotation argument. This means that I can't inadvertently apply the @Audit annotation to anything but a controller

The AuditAdvice class delegates the responsibility for the actual auditing to an AuditService. This is a dummy service, so instead of doing something useful like storing the audit event in a database, it simply adds it to a log file.

/**
* Audit this screen against the current user name
*
* It's more useful to put this info into a database so that that you can count visits to
* pages and figure out how often they're used. That way, you can focus your design on the
* popular parts of your application. The logger is just for demo purposes.
*/public void audit(String screenName) {

String userName = getCurrentUser();

logger.info("Audit: {} - {}", userName, screenName);

}

/**
* Get the current logged on user name by whatever mechanism available
*/private String getCurrentUser() {return "Fred";}

}

So, that's the code covered, all that's left to do now is to sort out the Spring config file and there's not much to do here. Firstly, as with any AOP application, you need to add the following AOP enabling line:

Finally, when you run the application the user's visit to the home page is recorded. When the user clicks on the help link the visit to the help page is also recorded. The output in the log file looks something like this: