Friday 27 September 2019

Spring boot: Aspect: Hello World Application


Step 1: Define Loggable annotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {

}


Step 2: Annotate controller method @Loggable annotation.
@RestController
@RequestMapping("api/v1/")
public class EmployeeController {

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

} 

Step 3: Create an Advice that should be called on all methods that annotated with @Around annotation.
@Configuration
@Aspect
public class LoggingAspect {

	@Around("@annotation(com.sample.app.annotations.Loggable)")
	public Object executionTime(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("Execution started");
		
		long startTime = System.currentTimeMillis();
		Object result = joinPoint.proceed();
		long endTime = System.currentTimeMillis();
		
		System.out.println("Total time taken is : " + (endTime-startTime) + " milliseconds");
		
		System.out.println("Execution Finished");
		
		return result;
	}

} 

As you see LoggingAspect class is annotated with @Aspect annotation.

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

} 


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 {

} 


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 {
		System.out.println("Execution started");
		
		long startTime = System.currentTimeMillis();
		Object result = joinPoint.proceed();
		long endTime = System.currentTimeMillis();
		
		System.out.println("Total time taken is : " + (endTime-startTime) + " milliseconds");
		
		System.out.println("Execution Finished");
		
		return result;
	}

} 


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>> all() {
		return ResponseEntity.ok(emps);
	}

}


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";
	}

}


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 


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 started
Total time taken is : 16 milliseconds
Execution Finished

You can download complete working application from this link.

Previous                                                    Next                                                    Home

No comments:

Post a Comment