Spring AOP (Aspect Oriented Programming)

 

AOP framework is one of the key components of the Spring framework. AOP provides a facility to enable middleware services with the Spring application in a loosely coupled manner. Middleware services are the additional services that makes our application to be executed in a stable manner in all the situations. Middleware services can be like transaction management, security, mailing, logging etc.

By using Spring AOP, we can write the actual application and middleware services codes separately and then we can link these both codes through any of the AOP’s implementation approaches (programmatic, declarative XML, annotations). The main aim of AOP is to separating services from the actual business code. In simple words, you can add some more functionality to your business code without writing anything extra in your actual code.

AOP Terminologies :

These are some central AOP concepts and terminologies. These terms are not spring specific but they are related to AOP.

– Aspect : It is the service class which contains advices, join points etc. For example, a logging module would be known as AOP aspect for logging. An application may have any number of aspects as per the requirement.

– Join point : It is the possible position in your business method where the advice can be applied. This point can be execution of a method or handling of an exception.

– Advice : This is the actual code which is invoked during the method execution either before or after by Spring AOP framework.

– Pointcut : It is a point or a condition to execute aspects for business methods by linking the advice with the join points.

– Introduction : It allows you to introduce new additional methods or attributes to the existing classes.

– Target Object : It is the object on which we will apply the services. It will always be a proxied object.

– AOP Proxy : If you add business along with your service, it will combine these both and finally delivers a proxy object which will always be a child object of business class.

– Weaving : Weaving means combining, it combines both your service and business at runtime in Spring AOP.

– Advisor : It is the combination of pointcut and advice.

Types of Advice :

These are different types of advices :

– Before advice : It executes before the method execution.

– After returning advice : It executes after the method execution only if method completes successfully.

– After throwing advice : It executes after the method execution only if method exits by throwing an exception.

– After (finally) advice : It executes after the method execution irrespective of it’s termination either normally or abnormally.

– Around advice : It executes before and after of the advised method execution.

Now let’s try to understand Spring AOP by a simple example of logging. For better understanding, we will use Spring dtd based AOP implementation which will be a programmatic approach with XML configuration.

Required JARs :

– AOP libraries

       ~ spring-aop
       ~ spring-aspects
       ~ aspectj
       ~ aspectjrt
       ~ aspectjweaver
       ~ aopalliance
       ~ cglib-nodep

– Other libraries :

       ~ spring-core
       ~ spring-context
       ~ spring-beans
       ~ commons-logging
       ~ spring-expression

Create a Bank class under business package in which there will be two simple methods defined.

Define a customized exception for the Bank class within the same package, InvalidAccountException class.

Now, create advice & pointcut classes under services package.

BeforeService.java >> This will log an information before the method executes.

AfterService.java >> This will log an information after the method executes successfully.

AroundService.java >> This will log informations before and after the method executes.

ThrowsService.java >> This will log an information along with the exception message if any exception occur in method execution.

If we will apply these services to our business without any condition, in such case these all services will execute for each method of the Bank class. But we want to execute services only for some methods. For doing this we have to use pointcut classes along with the advice in advisors.

PointcutForDeposite.java >> This will put a condition to the services to execute only for ‘balanceDeposite()’ method.

Now, come to the main XML configuration file (spring.xml in resources package).

Finally create a test class Client.java under client package.

The outputs for this example will be like :

 

Leave a Reply