Loading

Quipoin Menu

Learn • Practice • Grow

spring / AOP Overview
tutorial

AOP Overview

Imagine you run a restaurant. Every time a customer orders food, you need to do three things: log the order, check if the customer is VIP, and track cooking time. If you write this logic inside every chef's recipe, your code becomes messy and repetitive.

Aspect-Oriented Programming (AOP) solves this by letting you write these common tasks once and apply them wherever needed without touching the main business logic.

In Spring, AOP helps you separate cross-cutting concerns (like logging, security, transactions) from your core business code.

Key terms you will hear:
  • Aspect – the module that contains cross-cutting logic (e.g., logging aspect).
  • Advice – what action to take and when (before, after, around).
  • Joinpoint – a point in your program where advice can be applied (e.g., method execution).
  • Pointcut – an expression that selects one or more joinpoints.
  • Weaving – linking aspects with the main code (happens at runtime in Spring).

Here is a simple logging aspect in Spring:


import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

@Before("execution(* com.example.service.*.*(..))")
public void logBeforeMethod() {
System.out.println("Method called - Logging from AOP");
}
}
This aspect will run logBeforeMethod() before any method in the com.example.service package.
Two Minute Drill
  • AOP separates cross-cutting concerns from business logic.
  • Aspect = module containing cross-cutting code.
  • Advice = what and when (before, after, around).
  • Joinpoint = where advice can be applied.
  • Pointcut = expression that selects joinpoints.
  • Spring AOP uses runtime weaving (proxies).

Need more clarification?

Drop us an email at career@quipoinfotech.com