What is Aspect Oriented Programming:
Aspects help us modularize the crosscutting concerns like Logging, Security and Transactions etc.. A crosscutting concern is described as any functionality that touches multiple points of an application.
Logging is an example of this, as most of the methods in an application implement logging. Few methods can also have some security rules applied to them.
With AOP, you still 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’re applying the new feature. Crosscutting concerns can now be modularized into special classes called aspects. This has two benefits. First, the logic for each concern is now in one place, as opposed to being scattered all over the code base. Second, our service modules are now cleaner since they only contain code for their core functionality and secondary concerns have been moved to aspects.
AOP Terms
Like most other languages AOP has it's own jargon. It is defined in terms of Advice, Pointcut and Join Points.
Advice: Aspects have a purpose, the job that they are meant to do. In AOP terms the job of an Aspect is called an Advice.
Advice defines what an when of an Aspect in addition to describing the job that an aspect will perform.
advice addresses the question of when to perform the job. Should it be applied before a method is invoked? After the method is invoked? Both before and after method invocation? Or should it only be applied if a
method throws an exception?
Spring aspects can work with five kinds of advice:
- Before — The advice functionality takes place before the advised method is invoked.
- After — The advice functionality takes place after the advised method completes, regardless of the outcome.
- After-returning — The advice functionality takes place after the advised method successfully completes.
- After-throwing — The advice functionality takes place after the advised method throws an exception.
- Around — The advice wraps the advised method, providing some functionality before and after the advised method is invoked.
Join Points:
A join point is a point in
the execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.
Pointcuts:
An aspect doesn’t necessarily advise all join points in an application. Pointcuts help narrow down the join points advised by an aspect.
If advice defines the what and when of aspects, then pointcuts define the where. A pointcut definition matches one or more join points at which advice should be woven. Often you specify these pointcuts using explicit class and method names or through regular expressions that define matching class and method name patterns. Some AOP frameworks allow you to create dynamic pointcuts that determine whether to apply
advice based on runtime decisions, such as the value of method parameters.
Aspects:
An aspect is the merger of advice and pointcuts. Taken together, advice and pointcuts define everything there is to know about an aspect—what it does and where and when it does it.
Introductions:
Introductions allow us to add new methods and attributes to existing classes.
Weaving:
Weaving is the process of applying aspects to a target object to create a new proxied object. The aspects are woven into the target object at the specified join points. The weaving can take place at several points in the target object’s lifetime:
Introductions allow us to add new methods and attributes to existing classes.
Weaving:
Weaving is the process of applying aspects to a target object to create a new proxied object. The aspects are woven into the target object at the specified join points. The weaving can take place at several points in the target object’s lifetime: