AOP: Spring vs. AspectJ. Alejandro Lujan and Xin Fei. Outline. • Our project. •
Introduction to AOP. • Introduction to AspectJ. • Introduction to Spring's AOP.
Two AOP implementations: AspectJ and Spring A brief comparative analysis
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Outline • • • • • • • •
Our project Introduction to AOP Introduction to AspectJ Introduction to Spring’s AOP Main differences Experiment Results Final Recommendation
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Introduction: our project • Analysis and comparison of two Aspect Oriented Programming approaches: AspectJ and Spring • Methodology • Implement aspects on a dummy application, both on AspectJ and Spring • Perform a comparative quantitative and qualitative analysis of both implementations. • Time • Amount of code and configuration needed • Maintainability
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Introduction to AOP • AOP = Aspect-Oriented Programming • One of the methodologies of “separation of concerns” (SOC) • AOP complements OO programming • Modularization of cross-cutting concerns • Dynamically modify static OO models
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Crosscutting Concerns • These system-wide concerns that span multiple modules are called crosscutting concerns. • Classic examples: Logging, Transaction, Access Control
• A new unit of modularization -an aspect- that crosscuts other modules. Class item {
Log(); }
Authentication function Authentication function (ASPECT)
Class ShoppingCart {
Log(); }
Authentication function
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
AspectJ • AspectJ is an aspect-oriented extension to Java language • Overlay onto Java • Join Points: “points in the execution” of Java programs
• Small additions to Java • Pointcuts: pick out join points and values at those points • primitive pointcuts • user-defined pointcuts
• Advice: additional action to take at join points in a pointcut • Introduction: additional fields/methods/constructors for classes • Aspect: a crosscutting type • comprised of advice, introduction, • field,constructor and method declarations
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
AspectJ Join Points
Aspects
Methods and Constructors
Crosscutting type
Calls Executions Fields Gets Sets
Pertarget Perthis Percflow Percflowbelow
Exception handler Initializations
Pointcuts Primitive Call Execution Handler Get set Initialization This target Within code Cflow User defined Pointcut declaration Abstract Overriding Static
Advice Before After Around
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
AspectJ • Compile-time weaving Source Compiler
Executable
Weaver
Executable
Source
Source Aspect
Compiler
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Spring’s Native AOP Runtime weaving through Proxy classes
Class1
Class2
Bean reference
myMethod()
Class1
Bean reference
Proxy Class
Reference
myMethod()
myMethod()
Advice beforeMethod() afterMethod() afterThrow()
Class2
config.xml
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Spring’s AOP • Offers three different declarative and compilation models • Native (Original) Spring model: proxy classes, pure xml configuration. • @AspectJ declarations: proxy classes, annotations for advice and pointcut declaration • Full AspectJ integration: uses AspectJ’s compiler and weaver, allowing for full AspectJ features.
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Main Differences AspectJ Java Language Extension
Need for a compiler
Compile-time weaving Greater flexibility on join point definition
Spring Java-only components Only need to modify configuration files Runtime weaving Limited model: method join points only
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Experiment • Small application where users buy items from a store that maintains an inventory • Implemented aspects: • Trace log • Age checking for product purchasing
• Tools used: • Spring IDE 1.3.6 (eclipse plug-in) • AspectJ (eclipse plug-in)
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Results AspectJ Learning curve, Code complexity, Configuration
Spring Very similar
Weaving
Need to use their compiler
Plain java, use of their library
Flexibility
Greater flexibility, richer language
Limited on pointcut definition, rich enough according to them
Performance
According to (3), Spring’s overhead can be 8 to 40 times slower than AspectJ
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
Final Recommendation It depends! • If the application will use a framework, and Spring is an option, we consider that its AOP capabilities are very easy to learn and to apply. • If there is the need to exploit AOP to its maximum flexibility or performance is a very high priority, and a slightly steeper learning curve is not a problem, AspectJ is most likely the best option. • If you want the best of both (and don’t mind a little extra work) you can get AspectJ over Spring with almost no integrating effort. For more details, you’re welcome to read our report!!
AOP: Spring vs. AspectJ Alejandro Lujan and Xin Fei
References •
Articles: 1. http://www-128.ibm.com/developerworks/java/library/jaopwork1/ (spring 1.1.3) 2. http://www.javaworld.com/javaworld/jw-01-2007/jw0105-aop.html 3. http://docs.codehaus.org/display/AW/AOP+Benchmark
•
Spring 4. Reference: http://www.springframework.org/docs/reference/aop.html 5. Eclipse plugin: http://springide.org/project/wiki/SpringideInstall
Remember that Spring 2.0 introduces new features into AOP, so reviews or articles that refer to earlier versions will not give the most up to date information.