:::: MENU ::::

Spring AOP – Introduction


What is AOP ?

Aspect Oriented Programming(AOP) is just not a feature that Spring provide, It is a model of programming that built over substitute of OOP which entails breaking down program logic into distinct parts (so-called concerns). It does not replace or remove OOP, but it add a much more flexibility over that.


Few important terminology in AOP :
A program has a lot of points/parts that can be altered and modified like methods, fields, Constructors and Classes are some of the points in the code where an aspect can be introduced, these point/part of code is identified as Jointpoint. Jointpoint are well defined points in the execution flow of the code where you can add some aspects.

A simple expression that group the joint point’s to apply the aspect is called Pointcut.
for example the group of all methods in class XYZ which take String as a parameter.

Actual code that we want to execute and introduce in our system in a particular pointcut. It also specify when to execute the code. Advice defines the actual behaviour that need to be plugged in into the system.
Commonly available scenario when we apply the Advice are :
Before: Runs when a joint point reached but before computation proceed.
After : After the computation finished and before the control returns to the caller.
After Return : After the successful return to the caller.
After Throw : Runs when an error occurred, typically when an exception is thrown
Around : It’s a combination of Before and After advice. which runs before and after the computation.

Spring Framework uses AOP as mechanism for providing enterprise level services like logging, Transaction, Error handling and Security.


How to implement AOP with Spring Framework ?

  • Annotation – AspectJ
  • Schema Based approach
  • DTD Based approach (used in earlier version of Spring 2.1)

We will discuss all the implementation one by one with the previous example used in Spring IOC and we will add some cross cutting concerns in the core logic without touching or modifying the core logic of the application.

Spring AOP Setup :
For implementing AOP with Spring we need to add few dependent library file with Spring setup
AspectJ: http://www.eclipse.org/aspectj/downlo…  This need to be install in the system. SO donwload the jar file and click and install the AspectJ in your system and the browse to the AspectJ/lib directory and get the aspectjrt.jar and aspectjweaver.jar

AOP Alliance: http://aopalliance.sourceforge.net/
CGILIB: http://cglib.sourceforge.net/

You can follow the same steps and add the new .jar file in a new User defined library. Now we are ready to do some hands on in Spring-AOP.

Koushik Ghosh

Koushik Ghosh

Co-founder of CodeStrive. I am a developer in Java. I love coffee, music, coding, open source technology and playing Counter Strike. Feel free to connect me ..
Koushik Ghosh

Latest posts by Koushik Ghosh (see all)

Spring IoC and Dependency Injection
Spring JdbcTemplate
Pride that quarells at self-breath