July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Implementing AOP in Spring

  • February 25, 2013
  • By Manoj Debnath
  • Send Email »
  • More Articles »

Aspect Oriented Programming (AOP) has always been a fundamental component of the Spring framework. This programming model used to be a bit complex due to the exposure of the low level constructs to the Spring developer. Perhaps, for this reason programming Spring AOP rested in the hands of advanced developers. Lately, a lot has changed - Spring has evolved and so has AOP. In this article I will resort to a canonical explanation of AOP and on how to implement it in Spring. So, before getting hands on - a little detour.

Functional Programming

In case of Functional Programming (FP), problems are broken down into a module of functions, which carry out a particular unit of work. Each function talks to another through function calls. The whole concept is to separate the problem domain with a cohesive module of function.

Now, imagine a situation of a complex network of function calls. You get a bowl of spaghetti code!

spaghetti code

Spaghetti code

Object Oriented Programming

In Object Oriented Programming (OOP) the thought process of designing an application gets a paradigm shift. Different tasks are no longer modeled as a unit of function but rather as an individual object, which mirrors a different entity in a problem space.

As a result designing complex code got a much cleaner separation of concern. But still...

Table

A couple of problems persist.

  • A situation arose where not all of the tasks can be classified into an object or implementing such an object does not adhere to the business concern, per se.
  • Suppose there is a common procedure used by some/all of the objects, such as logging a message. Now, to implement that, one has to either define logMessage as the member function in each object that wants to log a message or create a separate Logger class with logMessage as a member static/non-static function, so that it can be used in the way the programmer wants either by inheriting or creating new Logger object or simply calling the static member function or by using dependency injection functionality through Spring.

Whatever the case, it is not a very good idea to design in the manner described. In the above case, observe that the most used object in the arena is actually the most unimportant one. The Logger object adds nothing to the business concern. It only supports the cause yet it gets the maximum attention from the object interaction. OOP is just fine but another level of modularity is sought to address this issue of pure separation of cross-cutting concern.

Aspect Oriented Programming

AOP leveraged the modularity of OOP paradigm to a whole new level. It provides separation of crosscutting concerns by introducing a new unit of modularization called an aspect. The aspect focuses on a particular crosscutting functionality and does not burden the core business classes of the application with crosscutting concerns. This is done with aspect weaver that composes the final system by combining core classes and crosscutting aspects through a process called weaving. AOP further refurbished OOP, which are easier to design, implement, and maintain.

AOP in Spring Framework

The core areas of Spring are dependency injection, enterprise service abstraction and aspect oriented programming. Dependency injection allows disparate components to be wired into a fully working application in declarative manner, enterprise service abstraction isolates stable application logic from volatile infrastructure code and AOP lets you separate the implementation of cross-cutting concerns from business concerns. This trio can be well integrated to offer a compelling solution in Spring.

AOP implementation in Spring

Software Dependencies:

For Aspectj programming

  • Aspectj: Aspectj compiler produces programs for any version of the Java platform, but the compiler itself requires Java2 (1.4 or later) to run.
  • Cglib: Code Generation Library, used to extend JAVA classes and implements interfaces at run time.
  • Aopalliance : A set of interfaces used by multiple frameworks such as Guice and Spring.
  • asm: All purpose Java bytecode manipulation and analysis framework used to modify existing classes or dynamically generate classes, directly in binary form.

For Spring framework

  • Spring:  Open source application Framework and Inversion of Control container for the Java platform.
  • Common-logging: Library to log information.

Java SE and Eclipse  (Note: any other IDE would be fine but here Eclipse is used).

Configuring Your Project for AOP

Install Java SE and Eclipse. Create user libraries in Eclipse with the following jars and include the library as follows.

Create aspectj user library and import the following jars

1.    aopalliance-x.x.x.jar

2.    cglib-nodep-x.x.x.jar

3.    aspectjrt.jar, aspectjweaver.jar (you may have to unzip aspectj-x.x.x.jar to get these two files)

4.    asm-x.x.jar

Create Spring user library and include the following jars

1.    commons-logging-x.x.x.jar

2.    All jar files from spring-framework-x.x.x/dist

Once you are done creating user library in Eclipse, you can reuse the library in any other project in which you wish to include the aspectj feature.

Note: User Library can be created in Eclipse from Windows->Preference, Expand Java->Build Path->User Libraries from the lists in the left of preference window.

Examples

The example contains four classes: Employee and Department as the model class, Main as the starting point of the application and LoggingAspect as the aspect class.

package org.simpleaop.model;
public class Employee {
       private String name;
       ...
       //Getters and Setters
}
 
package org.simpleaop.model;
public class Department {
       private String location;
       ...
       //Getters and Setters
}
 

Main Class demonstrates dependency injection in Spring.

package org.simpleaop.app;
//...import statements
public class Main {
       public static void main(String[] args) {
             ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
             
             Employee emp = context.getBean("employee", Employee.class);
             emp.setName("Jerry Mouse");
             
             Department dept = context.getBean("department", Department.class);
             dept.setLocation("Disney Land");
             
             System.out.println("Name :"+emp.getName());
             System.out.println("Department :"+dept.getLocation());
       }
 
}

Configuration file spring.xml demonstrates dependency injection and AOP configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
       
       <aop:aspectj-autoproxy />
       <bean name="employee" class="org.simpleaop.model.Employee"/>
       <bean name="department" class="org.simpleaop.model.Department"/>
       <bean name="loggingAspect" class="org.simpleaop.aspect.LoggingAspect" />
 
</beans>

LogginAspect class demonstrates the implementation of AOP in Spring. Note that it is nothing more than a simple POJO with the annotation @Aspect. Inside this class one can write as many advices (advice is a AOP terminology that means nothing more than a function prefixed with AOP annotation) as one wants to using the annotation like @Before, @After, @Within, @Around, @Pointcut etc.

package org.simpleaop.aspect;
//..import statements
 
@Aspect
public class LoggingAspect {
       
       @Before("execution(public String getName())")
       public void beforeAdvice(){
             System.out.println("Before advice. Runs before the function called");
       }
       //...other advices
}

@Before annotation simply means that beforeAdvice advice would run before any public String getName() function is executed. There are other variations of @Before annotation such as:

@Before("execution(public
String org.simpleaop.Employee.getName())")

The difference is that the advice now runs only before getName function of Employee class.

Using wild cards:

@Before("execution(public * get*())") 

calls any public function with any return type with no argument but function name prefixed by 'get'.

@Before("execution(public * get*(..))") 

calls any public function with any return type with zero/more argument but function name prefixed by 'get'.

@Before("execution(public * get*(*))") 

calls any public function with any return type with one/more argument but function name prefixed by 'get'.

For more examples and explanation visit the Spring aspect-oriented programming documentation.

AOP Usage

There are several occasions where AOP can be very handy, some of the most common use cases can be Logging and Profiling.

  • AOP can be very useful in transaction management where request processing methods can be wrapped using @Around advice where transactions can be rolled back immediately on failure.

  • An access control or security mechanism can be built with @Before advice and verify the access credentials before letting one in.

  • Exception wrapping can be done when business methods throw an exception. The @After throwing advice can be utilized to handle the situation. For example, if one wants to wrap all SQLException into data access layer exception and throw it back to service layer without handling it in every DAO method.

There are innumerable such AOP usages. The citations above just scratche the surface.

Conclusion

Spring is a popular lightweight framework for enterprise applications. AOP system based on interceptors and the proxy design pattern is included in the framework to replenish the needs of enterprise applications. Prior to Spring 2.0, Spring AOP was more complex. The new programming model based on AspectJ offers a better programming experience and enables programmers to write better code and customize aspects without difficulty. Once one has a fair grasp on the concept of dependency injection and AOP, the learning curve of Spring implementation of aspect-oriented programming in almost flat.


Tags: Functional programming, Spring, object oriented programming, OOP, Aspect Oriented Programming




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel