What is Spring AOP – Let’s explore

Spring AOP

Spring Series – Learn Spring AOP by Example

AOP Basics

In today’s complex application development the following points are always kept as a focus area.

  • Application should be developed as a combination of modular components.
  • All the components should be loosely coupled.
  • Components should be reusable.

The advantage of this approach is

  • Easy maintenance and enhancement of the application.
  • Components can be reused in the existing or new application.
  • Reduces the cost of maintenance and enhancement.

Now to address these concerns spring has developed the concepts like IOC and AOP. We have already discussed IOC so now we will concentrate on AOP.

Spring AOP

AOP targets the application functionalities (like logging, transaction etc) which are used throughout the application in different areas. Using AOP these cross-cutting functionalities are developed as a reusable component and injected into the application where ever required. And again the entire configuration is done using xml file.

Implementation of AOP

Following diagram shows a travel management system. ‘Ticket Booking’, ‘Hotel Booking’ and ‘Travel Planning’ are different functionalities. Now all of these functionalities require services like logging, transaction and security. So these are cross cutting concerns which come across all the modules. These concerns are developed based on the principle of AOP.

All the common concerns are developed as a plug-in, so it can be used in future modules as well. You just need to configure it so that the service is also available in your new modules. This is the power of aspect oriented programming (AOP).

AOP is also applied in enterprise level applications. In enterprise application, transaction, logging, security and many more features are there as cross cutting concerns. So all of these are developed as aspects and configured in xml file.

The biggest advantage is loose coupling. As all the concerns are self-managed and independent, so you can change and enhance them as per your requirement without touching the application code.

Travel Management System 

Now we will describe the AOP implementation by showing an example.

To start with, first we will define an interface and its implementation class. This is a simple application to compare two integer values and return the result.

Java is the interface definition to compare two integer values

package com.techalpine;

public interface CompareInt{

public String compare(int a,int b);


Java is the implementation class. It compares two integer values and returns the result

package com.techalpine;

public class CompareIntImpl implements CompareInt{

public String compare(int a, int b){

if (a>b)


return a +” is greater than”+ b;




return “b is greater than a”;




Now we will write a simple advice to be called before calling the compare (int, int) method. The advice will be discussed in details in the following chapters. Now you can imagine advice as a task which will be plugged into the application through xml configuration. And this will be called before the method is called. But there will not have any direct coding inside the implementation class. We will only take before advice for the implementation class.

package com.techalpine;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class LogBefore implements MethodBeforeAdvice{

public void before(Method method, Object[] args, Object target) {

System.out.println(“Before Calling the compare Method”);



Now we will discuss about the configuration file (Test-AOP.xml)

The file has three sections

  • The first section is the advice bean definition
  • The second section defines the implementation class
  • The third section defines the binding of the advice with the implementation class

<beans xmlns=”http://www.springframework.org/schema/beans”




<!– Advices –>

<bean id = “beforeCall”

class = “com.techalpine.LogBefore” />

<!– Implementation Class –>

<bean id = “compareImpl”

class = “com.techalpine.CompareIntImpl ” />

<!– Proxy Implementation Class –>

<bean id = “compare”

class = “org.springframework.aop.framework.ProxyFactoryBean”>

<property name = “proxyInterfaces”>



<property name = “interceptorNames”>





<property name = “target”>

<ref bean = “compareImpl”/>




Now the last part is the test class CompareTest.java. It loads the bean definition by using XMLBeanFactory class and then called the compare () method.

package com.techalpine;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.*;

public class CompareTest {

public static void main(String args[]){

Resource resource = new FileSystemResource(“./src/Test-AOP.xml”);

BeanFactory factory = new XmlBeanFactory(resource);

CompareInt com = (Adder)factory.getBean(“compare”);

String result = com.compare(12,10);

System.out.println(“Result = ” + result);



The output of the compare () method call would be as follows

Before calling the compare Method

12 is greater than 10


Spring AOP is very helpful when we try to separate business logic from cross cutting concerns. It is widely used in different forms to cover various aspects. AOP can also be implemented in other programming languages.


Please follow and like us:
Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + = 12

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email