12
Aspect Oriented Programming - AspectJ Radhika Rajput

Aspect Oriented Programming - AspectJ Radhika Rajput

Embed Size (px)

Citation preview

Page 1: Aspect Oriented Programming - AspectJ Radhika Rajput

Aspect Oriented Programming -

AspectJ

Radhika Rajput

Page 2: Aspect Oriented Programming - AspectJ Radhika Rajput

• Passive task like logging, transactions, security, caching etc

• Cross Cutting Concerns / Core Concerns

• Separating from business logic

Radhika Rajput
These passive activities which are necessary but not part of business logicSeparating Cross Cutting Concernsfrom business logic can be a major step towards writing an well designed decoupled code
Page 3: Aspect Oriented Programming - AspectJ Radhika Rajput

• Inheritance : Brittle

• Delegation : Cumbersome

• Aspect Oriented Programming

Radhika Rajput
Inheritance pops up in the mind straight away, we can inherit the common functionality and use it in our objects. But Inheriting the common functionality requires us to design us a base class. If we reuse this class in multiple places then modifying the class could be a tough job later on.Delegation is a better way of dealing with the cross Cutting Concerns. Remember Composition over Inheritance, (delegation and composition share a common concerns). But Then we would have to make calls to delegate objects at many places thus making it cumbersome.
Page 4: Aspect Oriented Programming - AspectJ Radhika Rajput

AOP• AOP is a technology for separating

crosscutting concerns into single units called aspects.

• Encapsulating crosscutting concerns.

• AOP builds on top of other programming paradigms: object-oriented, procedural or functional. It does not replace them

• AOP just saves you writing extra code

Page 5: Aspect Oriented Programming - AspectJ Radhika Rajput

• Define the common functionality and use it without modifying the underlying code to use the new feature.

• AOP : Modularize into Aspects, thereby creating a cleaner and decoupled code.

• With Aspects in place objects, AOP takes care of passive cross cutting concerns.

Radhika Rajput
define the common functionality in one place, but you can declaratively define how and where this functionality is applied without having to modify the class to which you are applying the new feature.AOP allows us to modularize cross cutting concerns into special objects called Aspects, thereby creating a cleaner and decoupled code. With aspects in place objects no longer have to worry about performing the passive cross cutting concerns as AOP takes care of it all.
Page 6: Aspect Oriented Programming - AspectJ Radhika Rajput
Page 7: Aspect Oriented Programming - AspectJ Radhika Rajput

Aspect-Jargons

• Joinpoint : A joinpoint is a candidate point in the execution of the application where an aspect can be plugged in.

• Advice : It is the actual implementation of our aspect.

• Pointcut : A pointcut defines at what joinpoints advice should be applied.

Page 8: Aspect Oriented Programming - AspectJ Radhika Rajput
Page 9: Aspect Oriented Programming - AspectJ Radhika Rajput

Weaving• Weaving is the process of applying aspects to a target

object.

• Aspects are woven into the target object at the specified joinpoints.

• Compile time

• Classload time

• Runtime

Radhika Rajput
This can be either a class you write or a third-party class to which you want to add custom behavior. Without AOP, this class would have to contain its primary logic plus the logic for any cross-cutting concerns. With AOP, the target class is free to focus on its primary concern, oblivious to any advice being applied.—Aspects are woven in when the target class is compiled. This requires a special compiler.Aspects are woven in when the target class is loaded into the JVM. This requires a special ClassLoader that enhances that target class’s bytecode before the class is introduced into the application.Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy class that will delegate to the target class while weaving in the aspects.
Page 10: Aspect Oriented Programming - AspectJ Radhika Rajput

AspectJ - Observer Pattern

Observer Pattern• Which objects are Subjects and which are Observers ?• When should the Subject send a notification to its Observers ?• What the Observer should do when notified ?

Page 11: Aspect Oriented Programming - AspectJ Radhika Rajput

References :

• http://en.wikipedia.org/wiki/Aspect-oriented_programming

• http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html

• https://eclipse.org/aspectj/doc/released/progguide/index.html

Page 12: Aspect Oriented Programming - AspectJ Radhika Rajput

Thank you