In this post, let us discuss about different basic Aspect Oriented Programming(AOP) terminologies and keywords which will help to understand the upcoming spring AOP tutorials.It is very much important to understand the basic AOP terms and usage first to understand the internals of AOP, it's implementation and usage using Spring framework.The AOP terminologies described here are not specific to Spring AOP but are generic terminologies used in any AOP framework.
AOP is software paradigm aims to achieve modularity using Cross Cutting Concerns.Cross Cutting Concerns can be explianed as any functionality that affects multiple points of an application.It helps us to fill the gaps in modular programming left in inhertiance or delegation. One of the key components in spring is spring AOP.The module helps us provide declarative enterprise services.
The modularization of concern is known as Aspect.In spring,Aspect is a class with special priviliges and are annotaed with @Aspect to represent Aspects.
While dealing with classes, we create a reference of another class and invoke methods on top of that.But an aspect behaviour is different then class.Instead of calling an aspect in any class by referencing it, we can define a configuration Aspect Configuration which tells which methods of which object this aspect isto be applied.And of course this all things are inside spring container and hence it is easy to use AOP with spring.
A joinpoint is a point in the execution of the application where an aspect can be plugged in.Joinpoint has information about actual method call that triggered the advice so that whichever method triggered the corresponding aspect can be traced. 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 code can be inserted into the normal flow of your application to add new behavior.
In spring AOP, basically joinpoint is applied to methods and we can treat joinpoints are synonomyous.But if we use Aspectj, even joinpoint can be applied to variables.
In AOP terminology the job of an aspect is called Advice.It defines both when and what of the aspect. 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./p>
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.
Pointcut is an AOP terminology for all the points in the execution of your code where you want this advice method to cut in.It defines the Where of the aspect.A pointcut definition matches one or more join points at which advice should be woven.Predicate Expressions are used with advice to set the target of execution. Example of Point cut Expression:
AOP Proxy is an Object created by the AOP framework to execute our service.At run time the Spring weaves our service and target object to create the desired proxy object.
According to OOPS, a method of different class can never execute unless and untill we invoke it.It is almost impossible.To achieve this in AOP, we use AOP proxy. For example, suppose class A is directly calling class B and class B is internally calling to C and adding some features and then returning to A.This is the concept of proxy.
In Spring an AOP proxy can be JDKDynamic proxy or CGLIB proxy.
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.It can take place at several points in the target object's life time
Compile time: Aspects are woven in when the target class is compiled. This requires a special compiler. AspectJ’s weaving compiler weaves aspects this way. Compile time weaving happens with our native aspectJ implementation with the help of ajc compiler.
Classload time: Aspects are woven in when the target class is loaded into the JVM. This requires a special ClassLoader that enhances that target class's byte-code before the class is introduced into the application. AspectJ 5’s load-time weaving (LTW) support weaves aspects in this way.
Run time: Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy object that will delegate to the target object while weaving in the aspects.
Thanks guys, for spending your time to read my article.I would love to hear your comments below.
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
2. Spring Boot Mvc Example
Contact us: firstname.lastname@example.org