🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this Spring AOP example, we will learn to use AspectJ @Before annotation in Spring-based applications. We can declare before advice in an aspect by using the @Before annotation.
Before advice methods annotated with @Before that will execute before the join point. These methods do not prevent the execution of the target method unless they throw an exception.
In this example, We will create a simple spring boot application, add a logging aspect and then invoke aspect methods based on pointcuts information passed in a @Before annotation.
AspectJ @Before Annotation Usage
The below snippet shows the usage of @Before annotation:
@Aspect
@Component
public class LoggingAspect {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
public void logBeforeAllMethods(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logBeforeAllMethods() : " + joinPoint.getSignature().getName());
}
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.getEmployeeById(..))")
public void logBeforeGetEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logBeforeGetEmployee() : " + joinPoint.getSignature().getName());
}
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.createEmployee(..))")
public void logBeforeAddEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logBeforeCreateEmployee() : " + joinPoint.getSignature().getName());
}
}
Let's demonstrate the usage of @Before annotation with a complete step-by-step example.
1. Creating a Spring Boot Application
There are many ways to create a Spring Boot application. You can refer below articles to create a Spring Boot application.
>> Create Spring Boot Project With Spring Initializer
>> Create Spring Boot Project in Spring Tool Suite [STS]
>> Create Spring Boot Project in Spring Tool Suite [STS]
Refer next step to create a project packaging structure.
Project Structure
Below project structure for your reference -
Maven Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<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>net.guides.springboot2</groupId>
<artifactId>springboot2-springaop-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot2-springaop-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<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>net.guides.springboot2</groupId>
<artifactId>springboot2-springaop-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot2-springaop-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Create an Aspect - LogginAspect.java
Let's create a LoggingAspect using a Java-based configuration. Here is an AspectJ annotated class and methods with pointcut information:
@Aspect
@Component
public class LoggingAspect {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
public void logBeforeAllMethods(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logBeforeAllMethods() : " + joinPoint.getSignature().getName());
}
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.getEmployeeById(..))")
public void logBeforeGetEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logBeforeGetEmployee() : " + joinPoint.getSignature().getName());
}
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.createEmployee(..))")
public void logBeforeAddEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logBeforeCreateEmployee() : " + joinPoint.getSignature().getName());
}
}
Create a Java POJO - Employee.java
package net.guides.springboot2.springaop.model;
public class Employee {
private long id;
private String firstName;
private String lastName;
private String emailId;
public Employee() {
}
public Employee(long id, String firstName, String lastName, String emailId) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
public long getId() {
return id;
}
public void setId(long 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;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
+ "]";
}
}
Create service methods - EmployeeService.java
Let's create a few service methods for Employee on which aspects need to be executed.
package net.guides.springboot2.springaop.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import net.guides.springboot2.springaop.model.Employee;
/**
* Employee Service
*
* @author Ramesh
*
*/
@Service
public class EmployeeService {
private List < Employee > employees = new ArrayList < > ();
public List < Employee > getAllEmployees() {
System.out.println("Method getAllEmployees() called");
return employees;
}
public Employee getEmployeeById(Long employeeId) {
System.out.println("Method getEmployeeById() called");
for (Employee employee: employees) {
if (employee.getId() == Long.valueOf(employeeId)) {
return employee;
}
}
return null;
}
public void addEmployee(Employee employee) {
System.out.println("Method addEmployee() called");
employees.add(employee);
}
public void updateEmployee(Employee employeeDetails) {
System.out.println("Method updateEmployee() called");
for (Employee employee: employees) {
if (employee.getId() == Long.valueOf(employeeDetails.getId())) {
employees.remove(employee);
employees.add(employeeDetails);
}
}
}
public void deleteEmployee(Long employeeId) {
System.out.println("Method deleteEmployee() called");
for (Employee employee: employees) {
if (employee.getId() == Long.valueOf(employeeId)) {
employees.remove(employee);
}
}
}
}
Test Spring AspectJ Configuration and Execution
Now let’s test whether the above-configured aspects execute on given pointcut information using Spring boot Application:
package net.guides.springboot2.springaop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import net.guides.springboot2.springaop.model.Employee;
import net.guides.springboot2.springaop.service.EmployeeService;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
EmployeeService employeeService = applicationContext.getBean(EmployeeService.class);
employeeService.addEmployee(new Employee(100 L, "ramesh", "fadatare", "ramesh@gmail.com"));
employeeService.getEmployeeById(100 L);
employeeService.getAllEmployees();
}
}
Output:
From the above diagram, it clears that aspect advice executed on relevant joinpoints.
To learn more about Spring AOP then check out the complete Spring AOP Tutorial.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment