Monday 30 September 2019

Spring boot Aspects: @Around advice

@Around advice is used to execute specific piece of code before executing the Join point and after executing the join point.

Example
@Around("@annotation(com.sample.app.annotations.Loggable)")
public Object executionTime(ProceedingJoinPoint joinPoint) throws Throwable {
 String methodName = joinPoint.getSignature().getName();

 System.out.println("Execution of the method " + methodName + " started.");

 long startTime = System.currentTimeMillis();

 // Proceed with the next advice or target method invocation
 Object result = joinPoint.proceed();

 long endTime = System.currentTimeMillis();

 System.out.println("Execution of the method " + methodName + " finished.");
 System.out.println("Execution of the method takes " + (endTime - startTime) + " milliseconds.");

 return result;
}

'joinPoint.proceed()' executes the next advice if available else target method.

Find the below working application.

Employee.java    

package com.sample.app.model;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public Employee(int id, String firstName, String lastName) {
  super();
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

HomeController.java

package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

 @RequestMapping("/")
 public String homePage() {
  return "Welcome to Spring boot Application Development";
 }

}

EmployeeController.java

package com.sample.app.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.annotations.Loggable;
import com.sample.app.model.Employee;

@RestController
@RequestMapping("api/v1/")
public class EmployeeController {

 private static List<Employee> emps = new ArrayList<>();

 static {
  Employee emp1 = new Employee(1, "ram", "Gurram");
  Employee emp2 = new Employee(2, "Sunil", "Dayananda");
  Employee emp3 = new Employee(3, "krishna", "Majety");

  emps.add(emp1);
  emps.add(emp2);
  emps.add(emp3);
 }

 @Loggable
 @RequestMapping(value = "employees", method = RequestMethod.GET)
 public ResponseEntity<List<Employee>> allEmployees() {
  return ResponseEntity.ok(emps);
 }

}

LoggingAspect.java

package com.sample.app.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;

@Configuration
@Aspect
public class LoggingAspect {

 @Around("@annotation(com.sample.app.annotations.Loggable)")
 public Object executionTime(ProceedingJoinPoint joinPoint) throws Throwable {
  String methodName = joinPoint.getSignature().getName();

  System.out.println("Execution of the method " + methodName + " started.");

  long startTime = System.currentTimeMillis();

  // Proceed with the next advice or target method invocation
  Object result = joinPoint.proceed();

  long endTime = System.currentTimeMillis();

  System.out.println("Execution of the method " + methodName + " finished.");
  System.out.println("Execution of the method takes " + (endTime - startTime) + " milliseconds.");

  return result;
 }

}

Loggable.java

package com.sample.app.annotations;

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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {

}

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
 public static void main(String[] args) {
  
  SpringApplication.run(App.class, args);
 }
}

application.properties
logging.level.root=WARN

pom.xml    

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>springbootApp</groupId>
 <artifactId>springbootApp</artifactId>
 <version>1</version>
 <packaging>jar</packaging>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>

 <name>springbootApp</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>


  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>


  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>


 </dependencies>
</project>

Total project structure looks like below.



Run App.java.

Open the url ‘http://localhost:8080/api/v1/employees’ in browser, you can see below messages in console.

Execution of the method allEmployees started.
Execution of the method allEmployees finished.
Execution of the method takes 15 milliseconds.

You can download complete working application from this link.
    

Previous                                                    Next                                                    Home

No comments:

Post a Comment