Tuesday, 14 May 2013

Aspects with Annotations in Spring

Heres a quick example to show how annotations can be used as join points in AOP. The code below simply times the execution of any method with the anotation @Time on it.

1. Define an annotation that can be placed over a method to time
package com.rizvn.timer;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE })
public @interface Time {}
2. Define an aspect that will look for @Time annotation on any method. It will intercept the method in order to time it.
package com.rizvn.timer;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TimeAspect
{
  @Around("execution(@com.rizvn.timer.Time * *(..))")
  public Object time(ProceedingJoinPoint pjp) throws Throwable
  {
    long start = System.nanoTime();
    Object result = pjp.proceed();
    long end= System.nanoTime();

    long duration = end - start;

    System.out.println("Duration: " + duration + "nano secs.");
    return result;
  }
}
3. Add the following to your config. Make sure to include the xmlns:aop namespace (line 5) and the aop schema locations (lines 11 & 12).
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
      xmlns:c="http://www.springframework.org/schema/c"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


    <!-- This enables aspects in the spring context -->
    <aop:aspectj-autoproxy />

</beans>
or use the @EnableAspectJAutoProxy if using spring 3.2's java config
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
...

}

13 comments:

  1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete