Spring AOP – @Around Advice

Spring AOP – @Around Advice

We have discussed in our previous tutorial, the concepts, and terminology of the Spring AOP. Around advice is one of the types of advices. It can be applied to a method before and after the method execution. It acts as the most powerful kind of advice.

Let’s understand the Around advice briefly with the help of an example.

Example of @Around Advice

Here, we are going to create an example of around advice using the @Around annotation.

Following are the steps to create an example of @Around advice:

  • Create a DAO class

In this step, we are going to create a DAO class name Service that contains business logic. In this class, we have defined a method named delay() that will produce a delay of 10 seconds in the output.

Service.java

import java.util.concurrent.TimeUnit;
 import org.springframework.stereotype.Component;
 @Component
 public class Service {
                 public String delay() {
                                 // produce a delay
                                 try {
                                                 TimeUnit.SECONDS.sleep(10);
                                     } catch (InterruptedException e) {
                                       e.printStackTrace();
                                 }
                                 return " Delay in seconds " ;
                 }
  } 
  • Create a class that performs component scanning

In this step, we are going to create a class named DemoAOP that performs component scanning.

DemoAOP.java

import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 @Configuration
 @EnableAspectJAutoProxy
 @ComponentScan( "com.app.SpringAOP_Around" )
 public class DemoAOP {
 } 
  • Create an aspect class for aspect logic

In this step, we are going to create a class named AspectClass, that contains the aspect logic (advice types).

AspectClass.java

import java.util.List;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.springframework.stereotype.Component;
 @Aspect
 @Component
 public class AspectClass {
                 @Around(" execution( * com.app.SpringAOP_Around.*.*()) ")
                 public Object aroundServiceMehod(ProceedingJoinPoint pjp) throws Throwable { 
                                 String str = pjp.getSignature().toShortString();
                                 System.out.println(" \n Executing the @Around advice on method : " + str);
                                 // begin the TimeStamp
                                 long start = System.currentTimeMillis();
                                 // execute the method
                                 Object reslt = pjp.proceed();
                                 // end the stopwatch
                                 long end = System.currentTimeMillis(); 
                                 // compute the result
                                 long duration = end - start;
                                 System.out.println(" \n The Total Duration is :" + duration / 1000.0 + "seconds");
                                 return reslt;
                 }
  } 
  • Create the main class

In this step, we are going to create the main class named App that contains the main method.

App.java

import java.util.List;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 public class App 
 {
     public static void main( String[] args )
     {
         AnnotationConfigApplicationContext appcontext = new AnnotationConfigApplicationContext( DemoAOP.class );
        Service serve = appcontext.getBean( "service", Service.class );
         System.out.println(" Main Program - @Around"); 
         System.out.println(" callling the delay method");
         String s = serve.delay();
         System.out.println(" \n Message is :" + s);
         System.out.println("done");
         appcontext.close();
          }
  } 

OUTPUT

The following output shows that the @Around advice is applied to the method delay(). It also shows the total duration of execution.

Spring AOP – @Around Advice

Around Advice using Logger

Logger – Logger is an interface used to log messages for a specific application. Through the Logger interface, we can also log operations. It is available in the org.apache.log4j package.

Following code snippet shows how to use Logger:

     private static Logger newlogger = Logger.getLogger(App.class.getName()); 

In the above code snippet, the newlogger is an object of the Logger class.

Let’s understand the concept of Logger with the help of an example.

Example of around advice using Logger

Here, we are going to create an example of Around advice using Logger. We are using the previous example with some modifications.

In the following example, we are going to replace System.out.println with the newlogger.info.

Modifications are done in the following classes:

AspectClass.java

import java.util.List;
 import java.util.logging.Logger;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.springframework.stereotype.Component;
 @Aspect
 @Component
 public class AspectClass {
                 private Logger newlogger = Logger.getLogger(getClass().getName());
                 @Around(" execution( * com.app.SpringAOP_Around.*.*()) ")
                 public Object aroundServiceMehod(ProceedingJoinPoint pjp) throws Throwable {
                                 String str = pjp.getSignature().toShortString();
                                 newlogger.info(" \n Executing the @Around advice on method : " + str);
                                 // begin the TimeStamp
                                 long start = System.currentTimeMillis();
                                 // execute the method
                                 Object reslt = pjp.proceed();
                                 // end the stopwatch
                                 long end = System.currentTimeMillis(); 
                                 // compute the result
                                 long duration = end - start;
                                 newlogger.info(" \n The Total Duration is :" + duration / 1000.0 + "seconds");
                                 return reslt;
                 }
  } 

App.java

import java.util.List;
 import java.util.logging.Logger;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 public class App 
 {
      private static Logger newlogger = Logger.getLogger(App.class.getName()); 
      public static void main( String[] args )
     {
         AnnotationConfigApplicationContext appcontext = new AnnotationConfigApplicationContext(DemoAOP.class);
        Service serve = appcontext.getBean( "service", Service.class);
        newlogger.info(" Main Program - @Around");
        newlogger.info(" callling the delay method");
        String s = serve.delay();
        newlogger.info(" \n Message is :" + s);
        newlogger.info(" done ");
         appcontext.close();
        }
  } 

Output

Spring AOP – @Around Advice

In the previous example, we have used Logger with around advice. We can also use it with any other advice types.