📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. What is a cross-cutting concern?
Below diagram shows how the concerns like logging, security, and transaction management are cutting across different layers here:
Examples of cross-cutting concerns:
- Logging
- Security
- Transaction management
- Auditing,
- Caching
- Internationalization
- Error detection and correction
- Memory management
- Performance monitoring
- Synchronization
2. Note on AspectJ
3. AOP Concepts and Terminology
Spring AOP + AspectJ Example
>> Create Spring Boot Project in Spring Tool Suite [STS]
<?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>
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 +
"]";
}
}
EmployeeService.java
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);
}
}
}
}
LoggingAspect.java
package net.guides.springboot2.springaop.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Aspect for logging execution.
*
* @author Ramesh Fadatare
*
*/
@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());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
public void logAfterAllMethods(JoinPoint joinPoint)
{
LOGGER.debug("****LoggingAspect.logAfterAllMethods() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.getEmployeeById(..))")
public void logAfterGetEmployee(JoinPoint joinPoint)
{
LOGGER.debug("****LoggingAspect.logAfterGetEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.addEmployee(..))")
public void logAfterAddEmployee(JoinPoint joinPoint)
{
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
}
Application.java
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(100L, "ramesh", "fadatare", "ramesh@gmail.com"));
employeeService.getEmployeeById(100L);
employeeService.getAllEmployees();
}
}
Output
4. Understanding AOP Concepts and Terminology with the Above Example
Aspect
@Aspect
@Component
public class LoggingAspect {
...
}
Join point
Advice
- Before advice: Advice that runs before a join point but that does not have the ability to prevent execution flow from proceeding to the join point (unless it throws an exception).
- After returning advice: Advice to be run after a join point completes normally (for example, if a method returns without throwing an exception).
- After throwing advice: Advice to be executed if a method exists by throwing an exception.
- After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
- Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
Pointcut
@Before("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
Target object
AOP proxy
5. Enabling @AspectJ Support
Enabling @AspectJ Support with Java Configuration
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
}
Enabling @AspectJ Support with XML Configuration
<aop:aspectj-autoproxy/>
6. Declaring an Aspect
Using XML Configuration
<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect">
<!-- configure properties of the aspect here -->
</bean>
package org.xyz; import org.aspectj.lang.annotation.Aspect; @Aspect public class LoggingAspect { }
Excellent Ramesh!!! Fantastic stuff
ReplyDelete