What is Aspect Oriented Programing?

In software design, the best way of simplifying a complex system is to identify the concerns and then to modularize them. In fact, the OOP methodology was developed as a response to the need to modularize the concerns of a software system. The reality is, though, that although OOP is good at modularizing core concerns, it falls short when it comes to modularize the crosscutting concerns. The AOP methodology was developed to address that shortfall. In AOP the crosscutting concerns are modularized by identifying a clear role for each one in the system, implementing each role in its own module, and loosely coupling each module to only a limited number of other modules.

Core concern

A core concern is one of the main concerns an application is written for.

Crosscutting concern

The majority of software applications we design will contain common functionality that spans layers and tiers. This functionality typically supports operations such authentication, authorization, caching, communication, exception management, logging and instrumentation, and validation. Such functionality is generally described as crosscutting concerns because it affects the entire application, and should be centralized in one location in the code where possible.

Modularizing crosscutting concerns

Aspect oriented programing provides a technique to modularise crosscutting concerns. There are different implementations of AOP available at present in software industry, Spring AOP, AspectJ AOP and others. In this post, I will briefly talk about aspectJ weaving.

Before we move on, Let’s review logging concern with AOP.

Implementation of a logging concern using AOP techniques:

<<Image courtesy from Ramnivas Laddad, author of the book AspectJ in action>>

The logging aspect defines the interception points needing logging and invokes the logging API upon the execution of those points. The client modules no longer contain any logging-related code.

Why AspectJ?

AspectJ is a seamless aspect-oriented extension to the Java programming language that enables clean modularization of crosscutting concerns, such as error checking and handling, synchronization, context-sensitive behavior, performance optimizations, monitoring and logging, debugging support, and multi-object protocols

AspectJ lets programmers define special constructs called aspects. An aspect is a central unit of AspectJ. It contains the code that expresses the weaving rules for crosscutting.

AspectJ supports the following types of weaving.

  • Compile time weaving
  • post-compile time weaving
  • Load time weaving

Compile time weaving

Compile-time weaving is the simplest approach. When we have the source code for an application, aspectJ compiler (ajc) will compile from source and produce woven class files as output. Aspects are weaved into the class files when they are compiled.

Following is the simple example for compile time weaving. I have used maven plugin to compile and run the code.

POM file

<project xmlns=”http://maven.apache.org/POM/4.0.0″
         xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
         xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd”>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.imaginea.suresh</groupId>
    <artifactId>aspect101</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.3</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.imaginea.suresh.HelloWorld</mainClass>
                </configuration>
            </plugin
        </plugins>
    </build>
</project>

Test program


 package com.imaginea.suresh;

public class HelloWorld {

public static void main(String[] args){
new HelloWorld().sayHello();
}

private void sayHello() {
System.out.println(“Hello AspectJ”);
}
}

 

Aspect


package com.imaginea.suresh;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AspectHello {

@Before(“execution (* com.imaginea.suresh.HelloWorld.sayHello(..))”)
public void trace(JoinPoint joinPoint) {
System.out.printf(“Before saying Hello ‘%s’%n”, joinPoint);
}
}
When we execute “mvn install“, maven plugin will take care of compilation with ajc and produce woven classes.

When running the HelloWorld program, we receive output as follows

Before saying Hello ‘execution(void com.imaginea.suresh.HelloWorld.sayHello())’
Hello AspectJ

In next posts, i will talk about other types of weaving with AspectJ

References

  • http://en.wikipedia.org/wiki/Aspect-oriented_programming
  • http://eclipse.org/aspectj/