May 26, 2019
Hot Topics:

Modularizing and Code Reuse Using AOP

  • December 18, 2006
  • By Ayyappan Gandhirajan
  • Send Email »
  • More Articles »

Create Aspect:

package aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

public abstract class AbstractBenchmarkAspect {
   public abstract void benchmark();

   public Object executeBenchmarkAdvise(ProceedingJoinPoint jp)
      throws Throwable{
      long startTime = System.currentTimeMillis();
      Object response = jp.proceed();
      long endTime = System.currentTimeMillis();
      System.out.println("TIME  TAKEN (in msecs): "+
         (endTime - startTime));
      return response;

Point cut:

       <pointcut name="benchmark"
                    "execution(* sample.LoanCalculator.*(..)))" />

The build file has been given; by using it, you can run both flavors of benchmarking. Set the environment variables for Ant and then run "ant" to build and run both flavors.


<project name="aspect" default="run">

   <target name="init">
      <mkdir dir="build"/>
   <target name="compile" depends="init">
         <classpath refid="build.classpath"/>

   <target name="run" depends="compile">
      <echo>Running without aspect...</echo>
      <java classname="sample.LoanClient2" fork="true">
         <classpath refid="build.classpath"/>

      <echo>Running with aspect...</echo>

      <java classname="sample.LoanClient" fork="true">
         <classpath refid="build.classpath"/>
         <jvmarg value="-javaagent:libaspectjweaver.jar"/>

   <path id="build.classpath">
      <pathelement location="build"/>
      <pathelement location="bin"/>
      <fileset dir="lib">
         <include name="*.jar"/>


Aspect-Oriented Programming has numerous advantages. Some of them are mentioned below:

  • Aspects are loosely coupled
  • Reusable
  • Maintenance is easy
  • The following kind of aspects can be built
    • Error handling
    • Functional Testing
    • Performance Testing
    • Access Control
    • Logging
    • Event notification


To give fair coverage of the topic I should list any negatives along with all the positives. On the downside, in my opinion, I found the learning curve steep. It takes a little longer to understand the concepts of joint point, advice and annotations style of programming. It also adds some latency to the target application, which might make you feel the performance hit.


Apart from AspectJ, there are other implementations also available. Some of them are Spring, Jboss, and AspectWerks. It's up to you to choose the implementation that is right for you. AOP is easier to understand and implement. It achieves code reuse and modularization at much greater levels. It removes the cross cutting concerns and makes application loosely coupled and maintenance easy.


About the Author

Ayyappan Gandhirajan holds a Master's degree in Software Systems from BITS, Pilani, India and a Bachelor's degree in Electronics & Communication Engineering from MK University, India. He has over eight years of profound software experience in domains of Travel, Telecom/Mobility, and e-commerce and technologies such as Spring, AOP, ESB, Web services, WS Security, and J2EE. He currently works for Perot Systems, India. Before joining Perot, he worked for five years with Hewlett-Packard ISO, India. He can be reached at ayyappan.gandhirajan@ps.net or G_Ayyapparaj@yahoo.com.

Page 2 of 2

Comment and Contribute


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



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date