In Spring AOP Before Advice is used to execute some code before a target method runs. It is defined using the @Before annotation and is commonly used for tasks like logging, validation, or security checks. This helps keep cross-cutting concerns separate from the main business logic.
- @Before advice runs before the execution of a target method (join point).
- It is mainly used for logging, validation, or authentication checks.
- If an exception occurs in the advice method, the target method will not execute.
Syntax:
@Aspect
public class BeforeExample {
@Before("execution(* com.xyz.dao.*.*(..))")
public void doAccessCheck() {
// ...
}
}
Steps to Implement Before Advice in Spring Boot
Let’s understand the steps required to implement @Before advice in a Spring Boot application, which executes before the target method runs.
Step 1: Create a Spring Boot Project
Open Spring Initializr and provide the following details:
- Group: com.beforeadvice
- Artifact: aop-before-advice-example
- Dependencies: Spring Web
Download the project and extract the ZIP file and Import the Project into IDE
Step 2: Add Spring AOP Dependency
Add the following dependency in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Step 3: Create Model Class
Create package com.beforeadvice.model and add Student.java class to store student details (firstName and secondName).
Student class
package com.beforeadvice.model;
public class Student {
private String firstName;
private String secondName;
public Student() {}
public String getFirstName() { return firstName; }
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getSecondName() { return secondName; }
public void setSecondName(String secondName)
{
this.secondName = secondName;
}
}
Step 4: Create Service Class
Create package com.beforeadvice.service and add StudentService.java. This class contains the method addStudent() which creates and returns a student object.
StudentService class:
package com.beforeadvice.service;
import com.beforeadvice.model.Student;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
public Student addStudent(String fname, String sname)
{
System.out.println(
"Add student service method called");
Student stud = new Student();
stud.setFirstName(fname);
stud.setSecondName(sname);
return stud;
}
}
Step 5: Create Controller Class
Create package com.beforeadvice.controller and add StudentController.java. This controller handles GET request /add and calls the StudentService method.
StudentController class:
package com.beforeadvice.controller;
import com.beforeadvice.model.Student;
import com.beforeadvice.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired private StudentService studentService;
@GetMapping(value = "/add")
public Student addStudent(
@RequestParam("firstName") String firstName,
@RequestParam("secondName") String secondName)
{
return studentService.addStudent(firstName,
secondName);
}
}
Step 6: Create Aspect Class
Create package com.beforeadvice.aspect and add StudentServiceAspect.java.
- Define a Pointcut to target service methods.
- Add @Before advice to run before the service method execution.
StudentServiceAspect class
package com.beforeadvice.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class StudentServiceAspect {
// the pointcut expression specifying execution of any
// method in com.beforeadvice.service.StudentService
// class of any return type with 0 or more number of
// arguments
@Pointcut("execution(* com.beforeadvice.service.StudentService.*(..)) ")
private void anyStudentService() {} // the pointcut signature
@Before("anyStudentService() && args(fname, sname)")
public void beforeAdvice(JoinPoint joinPoint,
String fname, String sname)
{
System.out.println(
"Before method:" + joinPoint.getSignature()+ "\n Adding Student with first name- "
+ fname + ", second name- " + sname);
}
}
Step 7: Run the Application
Run the project as Spring Boot Application and open the browser.
Output Explanation
- @Before Advice runs first and logs the method details and student names.
- StudentService.addStudent() method executes and creates the student object.
- The controller returns the student details as the response.