In almost every application you develop there are scenarios which when solved using Aspect Oriented programming (AOP) can result in a low code, efficient and elegant solution. I will be giving a basic outline of AOP in spring and in which scenarios it can be useful. When used properly AOP will be a powerful tool in you software development kit.
Aspect-Oriented Programming is a programming paradigm like Object-Oriented Programming (OOP). In OOP the key unit of modularity is the class, whereas in AOP the unit of modularity is the aspect.
What are Aspects?
Parts of Spring Aspect
Aspect-Oriented Programming is a programming paradigm like Object-Oriented Programming (OOP). In OOP the key unit of modularity is the class, whereas in AOP the unit of modularity is the aspect.
What are Aspects?
- Reusable blocks of code that are injected into applications code in runtime
- Powerful tools for adding behaviour
- Solve cross cutting concerns in one place
What is a cross cutting concern?
- Evaluate business requirements and look for words like every or always
- Look for system level requirements that apply to multiple business requirements
When?
Few examples
- In a logging routine that applies to every service method
- Transaction management is a good example of a crosscutting concern in J2EE applications.
- Specialized exception handlers or message translators used throughout the application
Why?
- Avoid code duplication
- Mixing of concerns
- Maintain application logic
Spring Aspects
- Leverages AspectJ for aspecting.
- Byte code modification (Run time interweaving).
- Dynamic proxy bases (That's why every spring manages bean has at least a proxy). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
Parts of Spring Aspect
- Joint point is a point in code of a program where execution of an aspect is targeted towards. This is your method or line of code where your annotation that the aspect is going to target.
- Pointcut is the expression that identifies that join point through some sort of regular expression matching.
- Advice is the code that you actually execute at a join point that was selected by the point cut. It is the cross cutting concern routine that we are applying to, a join point in our application.
- Aspect is a module that contains all the pointcuts as well as advice that is injected at runtime.
- Weaving: Linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
You can use either xml based or annotation based configuration to setup you pointcut and advice.
Types of advice
- Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
- After returning advice: Advice to be executed after a join point completes normally.
- After throwing advice: Advice to be executed if a method exits by throwing an exception.
- After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
- Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
Common Designators
- execution: expression for matching execution
- within: expression for matching within certain types
- target: expression for matching specific type
- @annotation: expression for matching a specific annotation
References:
- I found course Spring: Framework In Depth by Frank P. Moley III very useful not only for AOP but in general. It filled some holes in my knowledge and has nice examples and insights.
- Spring framework documentation is also very good.