How to Implement Cross Cutting Concern using AOP in Scala


In this blog, I have explained that how easily we can implement Cross Cutting Concern such as logging using Aspect Oriented Programming in Scala. These concerns have nothing to do with core business logic. Aspect Oriented Programming is used for encapsulating cross-cutting concerns into re-usable modules. Here we used AspectJ weaver to weave precompiled aspects into project output

First we defined an annotation style aspect scala code:-

/**
   * around execution of Sum
   */
  @Around("execution(@com.<wbr />knoldus.aspect.Loggable * *.*(..))")
  def aroundSum(joinPoint: ProceedingJoinPoint): Object = {
    val result = joinPoint.proceed
    println("Method:-" + joinPoint.toShortString() + " Input:-" + joinPoint.getArgs().toList.<wbr />mkString(",") + " Result:-" + result)
    result
  }

This aspect will print Method, its input and result detail, when a method is entered annotated with the @Loggable annotation and finished execution.

Let’s define the @Loggable annotation Java code

@Retention(RetentionPolicy.<wbr />RUNTIME)
@Target({ ElementType.METHOD })
public @interface Loggable {
}

Finally define a Sum class to see usage of Aspect

class Sum {
  @Loggable
  def checkSum(a: Int, b: Int): Int = a + b
}

We also need to add META-INF/aop.xml to configure aspect weaver.

<aspectj>

    <aspects>
        <!-- declare existing aspects to the weaver -->
        <aspect name="com.knoldus.aspect.AnnotationBasedAspect"/>

    </aspects>

    <weaver options="-verbose">
        <dump within="com.knoldus.*"/>
    </weaver>

</aspectj>

Complete source code can be found here CrossCuttingConcern_Scala.

Now run sbt test. You will see below message on console.

Method:-execution(Sum.checkSum(..)) Input:-2,3 Result:-5

Advertisements

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Agile, Java, Scala and tagged , , . Bookmark the permalink.

4 Responses to How to Implement Cross Cutting Concern using AOP in Scala

  1. Russ White says:

    Just curious… why not use scala macros for a case like this? It seems more appropriate and idiomatic.

  2. Reblogged this on Play!ng with Scala and commented:

    Cross Cutting Concern using AOP in Scala

  3. Prince says:

    Does not work. I downloaded source code . Then built sbt project “sbt eclipse”. called the sum method in another method. But It ran. but Aop did not work. Do i need to configure anything else? Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s