Spring AOP Aspect-Oriented Programming with Spring
Jeroen Rosenberg, Java Developer jeroen.rosenberg@gmail.com
Table of Contents
Table of Contents ‣ Why Aspect-Oriented Programming?
Table of Contents ‣ Why Aspect-Oriented Programming? ‣ Basic AOP Concepts
Table of Contents ‣ Why Aspect-Oriented Programming? ‣ Basic AOP Concepts ‣ Getting Started with Spring AOP
Table of Contents ‣ Why Aspect-Oriented Programming? ‣ Basic AOP Concepts ‣ Getting Started with Spring AOP ‣ Benefits and Drawbacks
Table of Contents ‣ Why Aspect-Oriented Programming? ‣ Basic AOP Concepts ‣ Getting Started with Spring AOP ‣ Benefits and Drawbacks ‣ When to Use Spring AOP?
Aspect-Oriented Programming (AOP) enables modularization of cross- cutting concerns
Cross-cutting Concerns
Cross-cutting Concerns ‣ Logging ‣ Validation ‣ Transaction Management ‣ Security ‣ Caching
Code Tangling
Code Tangling Coupling of different concerns
Code Scattering
Code Scattering Same concern spread across modules
The encouraged approach for implementing cross-cutting concerns
The encouraged approach for implementing cross-cutting concerns
1. Solve core problem The encouraged approach for implementing cross-cutting concerns
1. Solve core problem The encouraged approach for implementing cross-cutting concerns
1. Solve core problem 2. Write aspects The encouraged approach for implementing cross-cutting concerns
1. Solve core problem 2. Write aspects The encouraged approach for implementing cross-cutting concerns
1. Solve core problem 3. Weave at runtime 2. Write aspects The encouraged approach for implementing cross-cutting concerns
AOP Terminology
AOP Terminology ‣ An aspect is a module that encapsulates advice and pointcuts
AOP Terminology ‣ An aspect is a module that encapsulates advice and pointcuts ‣ An advice is code to be executed at a join point selected by a pointcut
AOP Terminology ‣ An aspect is a module that encapsulates advice and pointcuts ‣ An advice is code to be executed at a join point selected by a pointcut ‣ A pointcut is an expression that selects one or more join points
AOP Terminology ‣ An aspect is a module that encapsulates advice and pointcuts ‣ An advice is code to be executed at a join point selected by a pointcut ‣ A pointcut is an expression that selects one or more join points ‣ A join point is a point in the execution of a program
Spring AOP Java-based AOP framework
Spring uses dynamic proxying for aspect weaving
Calls on the object reference will be calls on the proxy and the proxy delegates to all of the relevant advices
Regular method invocation Calls on the object reference will be calls on the proxy and the proxy delegates to all of the relevant advices
Regular method invocation Proxied method invocation Calls on the object reference will be calls on the proxy and the proxy delegates to all of the relevant advices
Spring offers 3 ways to accomplish AOP support Annotation based Schema based Schema based with AspectJ with AspectJ without AspectJ Very easy to Bloated Easy to configure configure configuration Horizontal class Horizontal class Horizontal class relationships at relationships at relationships at code level deployment level deployment level Degrades Enables portability Enables portability portability
Configuration Schema based aspect configuration without AspectJ
Sample XML configuration
Demo
Benefits No special compiler required to allow per-object interception
Drawbacks Lower performance
AOP Suitability Cost-effectiveness Code reduction Aspectizability Obliviousness Crosscutting Concern Evolvability Modularity Reliability Logging V V V V V Validation V V V V V V V ? Transaction Management V V V V V V V V Security V V V ? V ? ? Caching V V V ? ? V ? V
Modularization of Schema based cross cutting concerns aspect configuration Auto-proxying for aspect weaving Summary

Spring AOP

Editor's Notes

  • #33 Crosscutting. Does the concern have a crosscutting nature? Aspectizability. Can the concern be resolved by AOP? Obliviousness. Is neither the existence nor the execution of the Aspect code apparent by examining the base code (Which allows greater Separation of Concerns)? Code reduction. Does it reduce code? Reliability. Does it reduce the chance of making mistakes? Modularity. Does it reduce tangling (multiple concerns intermixed) and scattering (spread of code for a concern)? Evolvability. Does it make it easier to modify the implementation when requirements change? Cost-effectiveness. Do the benefits (Code Reduction, Reliability, Modularity, Evolvability) outweigh the costs (possible increase of complexity and/or decrease of performance)?