Joe Conley Tagged java Random thoughts on technology, business, books, and everything in between jpc2.org/name/java AOP and AspectJ Introduction and Quickstart <p><a href="https://en.wikipedia.org/wiki/Aspect-oriented_programming">Aspect-oriented Programming</a> (AOP) is a fairly novel concept in the world of programming. Most coders (in my generation at least) were brought up on <a href="http://searchsoa.techtarget.com/definition/object-oriented-programming">Object-oriented programming</a>. You define entities using classes with properties and methods (behavior), these entities talk to each other, and before you know it you’ve created an entire universe of objects doing your bidding.</p> <p>What are some issues with that approach? One problem is dealing with behaviors that should be shared by most or all objects in your universe, like logging. You could simply create a public global object and let your classes access that, but then your class definitions get muddled by logic that’s not directly related to the object. This is a recipe for disaster, we want our code to be small and modular, not big and monolithic!</p> <p>AOP to the rescue! AOP and it’s most popular implementation <a href="https://eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html">AspectJ</a>, provide a nice framework for abstracting out cross-cutting functionality and having an OOP framework use those concerns. Modularizing functions like logging, security, and transactionality can then lead to a much cleaner codebase. It also forces the developer to think about what services are core to the application and which are orthogonal and can be split out into its own module. All good things!</p> <p>Of course there are tradeoffs to using AOP. It adds complexity to the compilation process as it typically modifies bytecode to create proxy methods that execute the modular code. Defining pointcuts that match one or more class definitions or methods could lead to unexpected behavior as the codebase evolves. Ultimately, I think these tradeoffs are minimal in comparison to the gains in code stability. Every tool has tradeoffs, so it’s important to always be mindful of the technology you’re choosing as you’re building software.</p> <p>Implementing such a framework, however, can be a daunting task for even the bravest of souls. I spent the better part of this week trying to tame the dragons of AspectJ, Maven, and Spring. So to aid any other weary travelers who just want a dead simple example of AspectJ working in Java with annotations, <a href="https://github.com/josephpconley/aspectj-java-quickstart">here it is</a>. Enjoy!</p> Fri, 18 Nov 2016 00:00:00 +0000 jpc2.org/2016/11/18/aop-maven-quickstart.html jpc2.org/2016/11/18/aop-maven-quickstart.html