Saturday 28 September 2019

Spring boot: Aspects: Before Advice


@Before advice is used to execute specific code before executing the JoinPoint that is selected by the PointCut expression.

@Before("@annotation(com.sample.app.annotations.Loggable)")
public void logMe(){
         System.out.println("New Request Execution started");
}

Above snippet executes before calling the actual method that is annotated with @Loggable annotation.

Find the below working example.

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.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Configuration;

@Configuration
@Aspect
public class LoggingAspect {

 @Before("@annotation(com.sample.app.annotations.Loggable)")
 public void logMe(JoinPoint joinPoint){
  String methodName = joinPoint.getSignature().getName();
  
  System.out.println("Calling " + methodName + " method");
 }

}


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

}


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


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 message in console.

Calling allEmployees method

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment