The long way through Software Craftsmanship

Annotations and Aspects in Java

Apr 21, 2015 - 2 minute read - Comments - javaaspectaspectjspringannotationsnippet

I’ve written a sample project with an annotation and an aspect to intercept it and decorate it.

The code provides a way of doing try|catch, specifying an exception to be caught.

The source code is available here

In the process

This has taken me approximately one hour to prepare the spike and half more to refactor and massage a bit.

If you want to see the raw details, the refactor has been committed step by step (the spike hasn’t). The unmassaged code can be found here. The massaged version is at the tip of the arrow

More information

Regarding annotations (like @Override):

Regarding aspect (based on AspectJ):

  • This question has been my main source of information / inspiration for the spring + aspectj configuration

Conclusions

One of the drawbacks that I find to common examples or tutorials is that they’re not complete. They show you part of the information, but not the whole. While this helps most of the readers, there are library incompatibilities, defects, specific versions, etc. This is why I like to post the whole solution, including pom.xml (dependencies) and tests.

Having less documentation for developers can be acceptable if you have tests / more tests; of course, as long as other developers can follow your thought while developing.

Logging is a feature What legacy code is about