April 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Modularizing and Code Reuse Using AOP, Page 2

  • 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;

@Aspect
public abstract class AbstractBenchmarkAspect {
   @Pointcut
   public abstract void benchmark();

   @Around("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:

<aspectj>
   <aspects>
      <concrete-aspect
       name="aspect.BenchmarkAspect"
       extends="aspect.AbstractBenchmarkAspect">
       <pointcut name="benchmark"
                 expression=
                    "execution(* sample.LoanCalculator.*(..)))" />
      </concrete-aspect>
   </aspects>
</aspectj>

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.

Build.xml

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

   <target name="init">
      <mkdir dir="build"/>
   </target>
   <target name="compile" depends="init">
      <javac
         srcdir="src"
         destdir="build"
         source="1.5"
         debug="true">
         <classpath refid="build.classpath"/>
      </javac>
   </target>

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

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

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

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

Advantages

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

Disadvantages

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.

Summary

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.

References

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.

 

 


Sitemap | Contact Us

Rocket Fuel