Why we need AOP?

One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don’t want to, AOP is extra feature for Spring IoC to provide a very capable middleware solution.

Those who are familiar with filter concept in servlet/jsp or interceptor concept in struts 2.0 will be easily understand AOP. Also they can clearly differentiate advantages over both filters and interceptors.

We are going to see this from very basic. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

Let’s say we have class Customer with methods m1 and m2. Clients can call out these methods. For logging purpose I need to see when my m1/m2 is getting called and when it is exited.

With very basic programming knowledge we can do below:

  • But imagine we have many methods and then need to add everywhere entry and exit.

  • One more big issue is when we need to change something then need to change everywhere across project.

  • We can’t plug and play some operations before and after method execution here.

  • In industry we need to add transaction mechanism for all methods starts with save or all methods performing insert update delete operations.

  • In industry we log time date year for every method.

  • Also we must check access of a user for every functionality that cannot be done every method.

  • All these problem will be resolved by AspectJ programming.

  • Advantages we will see later once we learn AOP concept completely.