Wednesday, June 6, 2012

NOW I Get It: Aspect Oriented Programming

After going through the Aspect related chapter of Spring in Action the concept of Aspects finally "clicked" for me.  This is after reading the Wikipedia on it, the Spring documentation and various other articles as well.

To summarize:

  • Some things (like logging) need to be executed everywhere, even in modules who's primary purpose isn't logging.  Aspects address this issue.
  • An Aspect is just a bit of code (with Spring it can even be a POJO) that is configured (typically with an annotation or XML) to run before, after, or around a join point (for now, assume it's a fancy word for method).
  • For the canonical example of logging, you can create an aspect to log something like "Calling method x with parameters x, y and z" before a method runs and "Returning from method x with return value y" afterwards.  
    • This separates the logged class from the logging system entirely and results in a second class logging the first one.  
    • If you want logging statements in the middle of a method, too bad, refactor to call a helper method (thus creating a new join point) and log there.
Hopefully presenting the gist of it with concrete examples will help everyone's understanding.

No comments:

Post a Comment