In this tutorial we  are going to understand what is AOP Framework, how AOP Framework deals with the cross-cutting concerns and different types of AOP terminologies.

AOP Framework :

Aspect Oriented Programming (AOP) is a new programming paradigm.  If any language follows POP then it is a Procedural Oriented Programming language. Similarly, if a language follows OOP methodology then it is OOP language. If any programming language or Framework follows AOP methodology then it is a AOP based language or framework.

[highlight color=”blue”]AOP is not a programming language, Its a methodology.[/highlight]

For example, Aspect is a programming language which supports AOP.  Similarly Spring Framework supports AOP.

To understand better AOP framework, lets take an example like Triggers in SQL. In SQL; a trigger is created for a specific action such as insert, update or delete on a table or a view. This trigger then executes at a chosen scenario. AOP is something similar like that. Here is an Aspect created, which executes on a chosen scenario such as before, after, around and so on.

AOP Framework Cross cutting

In any application development, in general the functions can span across the multiple points. These are called cross-cutting concerns. These cross-cutting concerns are conceptually separate from the application’s business logic, they are typically embedded directly with in the application.

The above diagram represents a typical banking application that is broken into modules (Deposits, Payment, Misc Services). Each module is responsible to provide the services for its particular domain.

To achieve some generic functionalities (like giving security for all business components), on top of the business components we add some extra functionalities like security, transaction or statistics.

These functionalities are generic for all business operations. And these functionalities are called as cross cutting functionalities/concerns/logic. Cross cutting concerns are also called secondary logics, where as business logics are called primary logic.

We can achieve these cross cutting functionality through OOP also.

Adding cross cutting concerns with OOP:

We can apply the cross cutting concerns with OOPs also. To achieve cross cutting in OOPs we can go with either IS-A or HAS-A relationship. Because if we directly mix the cross cutting concerns with the business logic in a same class the following problems will occur.

  1. If another class of application need some cross cutting logic then we need to mix cross cutting logic in that class also with business logic. It means cross cutting logic becomes duplicated (redundancy) .
  2.  If any changes are needed in a cross cutting logic then we need to modify it at multiple classes. It means maintaining a cross cutting logic is hard.
  3. If cross cutting logic & business are mixed then the logic becomes a complex logic.

In order to overcome the above problems, we can separate primary logic (Business logic) & secondary logic(cross cutting logic) & then we can mix them at runtime with the help of  either is-A or has-A relation of OOP.

Cross cutting with Is-A relationship :

[sourcecode language=”java”]

public class Service {

    void authenticate() {
//authenticate
    }

    void logging() {
//logging
    }

    void transaction() {
//transaction
    }
}

public class Withdraw extends Service {

    public double withdrawMoney() {
        authenticate();
        logging();
        transaction();
//Business logic
    }
}

[/sourcecode]

Problems with Is-A Relationship :

If we apply is-A relation then 2 problems are identified.

  1. If we extends a business class from Service class then that business class is loosing an opportunity of extending one more business class.
  2. If one service method is not  called then that business logic will loose the service.

Cross cutting with Has-A relationship :

[sourcecode language=”java”]

public class Withdraw {

    Service s = new service();

    public double withDrawMoney() {
        s.authenticate();
        s.logging();
        s.transaction();
//business logic
    }
}

[/sourcecode]

Problems with Has-A Relationship :

In this Has-A relation , a business  class has an opportunity  to extend one more business class ,but if a service method is not called the business logic will loose that service.

So that by applying the cross-cutting in OOPs, we are facing the above problems. Lets examine the cross-cutting with AOP Framework.

Adding cross cutting concerns with AOP Framework:

Here is the major advantage of AOP Framework comes, In AOP methodology, adding the cross cutting logic to the business logics will be taken care by a third person.

As a programmer, we are only responsible to develop cross cutting logic & business logic separately, but we are not responsible to add them explicitly.
A third person (containers) at runtime will attach cross cutting logics to business logics.

With this AOP approach, we are not going to apply Is-A relation / Has-A relation explicitly so, comparatively AOP is a better approach then OOP for adding cross cutting logic to business.

To understand the AOP functionality, you need to aware the basic AOP Terminology. Here are the basic AOP terminologies.

AOP Terminology:

1)Aspect :

  • Aspect is a term which denotes or represents a cross cutting concern.
  • Aspect only represents a cross cutting functionality, but it is not a implementation of that functionality.
  • For example, a transaction cross cutting  concern is denoted as a transaction aspect .

2)Advice:

  • An advice contains actual implementation of a cross cutting concern.
  • We need to assume aspect like a class and advice like the method of a class.
  • For example, a transaction aspect is implemented as a transaction advice.

3)join-point:

  • It is a point at which advice are mixed into business logic.
  • While developing AOP application cross cutting logic  & business logic, both are defined separately.
  • At runtime cross cutting logic will be attached to business logic. At attaching point Is called join point.

There are 2 types of join point

  • Instantiation join point
  • Method access join point

Method access join point means an advice is joint to a business method, when that business method is called at runtime.

4)Point-cut:

  • A Point cut is a collection of join points.
  • Spring framework  supports only method access join point .
  • An application can have multiple classes and each class can have multiple methods, but all methods and all classes doesn’t need cross cutting logic.
  • A point cut identifies whether a particular method of a particular class is matching to a particular criteria or not if matched then that method access join point as eligible to attach the services.

5) Weaving:

  • A business class object is called as a target object.
  • A target object doesn’t contains cross cutting logic. So it is called a pre-AOP object.
  • At runtime a proxy class is created for the business class and in that advice are mixed into business methods next, an object of proxy classes will be created and it is called a proxied object.
  • The process of converting a target object to a new proxied object by adding the advice is called Weaving.
  • Weaving will be done at runtime by a predefined weaver  class.

6) Advisor:

  • A point-cut only knows about which method access join point are eligible to get cross cutting logic ,but it doesn’t knows what cross cutting logic is required at that join point .
  • An advisor, joint required advices to a point-cut, so an advisor is a combination of a point-cut and advice.

7) Introduction:

Introduction is a process of adding new properties of new methods to the business class at runtime while creating a proxy class internally.

In the above terminology of AOP, Aspect and join-point are not responsible physically in the form of classes, but remaining like, Advice, point-cut, weaver ,advisor & introduction are responsible, physically in the form of classes in a project.