In this tutorial, we
are going to introduce you to AspectJ programming.
Using Aspect Oriented Programming(AOP), we can introduce new features in an existing application without changing its design.
Spring Framework provides support for working with Aspect Oriented Programming(AOP)in an enterprise application.
To understand more about AspectJ programming, we must understand the concept of elements in aspect oriented programming.
The elements in aspect oriented programming are as follows -
Joinpoint
Pointcut
Advice
Joinpoint
An application code may have many several points in it at which an important module can be introduced. These points could be a constructor, method, field or a class.
These points are known as a joinpoint, where an important module could be introduced. To understand the concept of joinpoint, let's take an example -
Imagine you have an application in which you want to introduce a piece of code that checks the amount of free memory before a new object is created.
As the creation of a new object is triggered by the constructor of the class. Hence, the point at which this memory check module could be inserted is the constructor of the class,
which becomes the joinpoint.
Advertisement
Pointcut
Once you have located the joinpoint in your application code where an important module could be executed,
the next step is to introduce this module in the application on the fulfilment of a particular condition or a situation.
This condition is specified using an expression known as pointcut.
For example - A class may have several methods defined in it but if we want to introduce a piece of code only before the execution of methods that takes a String
object then we could express this situation using a pointcut expression.
Advice
Advice us to specify the piece of code or a module which needs to be introduced or executed whenever a joinpoint is called by the system.
Not just this, but using advice we can even specify a specific time frame at which this module will be introduced in the system,
by using various kinds of advice such as -
Before - This kind of advice allows a new module to be executed before the joinpoint is executed(when this joinpoint gets called).
After - This kind of advice allows a new module to be executed after the joinpoint is executed(when this joinpoint gets called).
Around - This kind of advice allows us to specify whether or not a joinpoint is executed(when this joinpoint gets called).
Throws - This kind of advice allows a new module to be executed when an exception is thrown.
These important elements of aspect oriented programming such as joinpoint, pointcut and advice are implemented by AspectJ programming language.
In the next tutorial, we are going to explain how to create a small application using AspectJ programming and its use.