LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1718 min read

Aspect-Oriented Programming (AOP) Basics

Understand cross-cutting concerns and how Spring AOP lets you apply behavior like logging across many classes without duplicating code.

Introduction

Some behaviors, like logging, security checks, or transaction management, do not belong to any single class; they cut across many classes in your application. Sprinkling the same logging or security code into every method is repetitive and error-prone. Aspect-Oriented Programming (AOP) lets you define that behavior once and apply it declaratively wherever it is needed.

What You Will Learn
  • What a cross-cutting concern is
  • The core vocabulary of AOP: aspect, advice, join point, pointcut
  • How to enable AOP in a Spring application
  • How to write a simple @Aspect class

What Is a Cross-Cutting Concern?

A cross-cutting concern is a piece of functionality that applies broadly across many, otherwise unrelated, parts of an application. Logging every service method call, checking permissions before an operation runs, and wrapping methods in a transaction are all classic examples. Without AOP, you would have to hand-write that logic inside every single method that needs it.

Logging

Recording when a method is called, with what arguments, and how long it took.

Security

Checking that the current user is authorized before a method executes.

Transactions

Wrapping a method in a database transaction that commits or rolls back automatically.

Core AOP Vocabulary

TermMeaning
AspectA class that encapsulates a cross-cutting concern, marked with @Aspect
Join pointA point during execution, such as a method call, where an aspect can be applied
AdviceThe action an aspect takes at a join point, e.g. code that runs before a method
PointcutAn expression that selects which join points a piece of advice applies to

Enabling AOP

To use annotation-driven AOP, add the spring-boot-starter-aop dependency (or spring-aspects in a plain Spring project) and annotate a configuration class with @EnableAspectJAutoProxy.

AppConfig.java
@Configuration
@ComponentScan(basePackages = "com.example.app")
@EnableAspectJAutoProxy
public class AppConfig {
}
Good to Know

Spring Boot applications with the AOP starter on the classpath enable this automatically, so you rarely need to add @EnableAspectJAutoProxy by hand in a Boot project.

Your First Aspect

An aspect is a plain class annotated with @Aspect and registered as a Spring bean with @Component. Inside it, an advice method is annotated with something like @Before and a pointcut expression that describes which methods to intercept.

GreetingService.java
@Service
public class GreetingService {
public String greet(String name) {
return "Hello, " + name + "!";
}
}
LoggingAspect.java
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.app.service.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Calling method: " + joinPoint.getSignature().getName());
}
}
Main.java
GreetingService greetingService = context.getBean(GreetingService.class);
System.out.println(greetingService.greet("PrograMinds"));
Console Output

Click Run to see what this code prints.

The pointcut expression execution(* com.example.app.service.*.*(..)) matches any method, with any return type, on any class directly inside the com.example.app.service package, regardless of its arguments.

How It Works Under the Hood

Spring AOP is proxy-based. When a bean matches a pointcut, Spring wraps it in a proxy object at startup. Calls made through the proxy are intercepted so the advice can run, and then the call is forwarded to the real, target object. This is also why calling one method on the same class from another method on that same class does not trigger the aspect: that internal call bypasses the proxy entirely.

Common Mistakes

Avoid These Mistakes
  • Forgetting @EnableAspectJAutoProxy (or the equivalent Spring Boot starter) so the aspect never actually runs.
  • Expecting an aspect to trigger on a self-invocation, a method calling another method in the same class, which bypasses the proxy.
  • Writing an overly broad pointcut that matches unrelated classes and produces noisy, unintended behavior.
  • Putting heavy or slow logic inside advice, which then runs on every matched method call and slows the whole application down.

Best Practices

  • Use AOP for genuinely cross-cutting behavior, like logging or auditing, not as a substitute for normal method calls.
  • Write focused pointcut expressions that target a specific package or annotation rather than an entire application.
  • Keep advice methods lightweight and fast since they run on every matching call.
  • Document non-obvious aspects clearly, since their behavior is not visible directly in the classes they affect.

Frequently Asked Questions

No. Spring AOP is a simpler, proxy-based subset that only intercepts method calls made through Spring beans. Full AspectJ can weave aspects into any code, including field access and object construction, but requires a separate compile-time or load-time weaving step.

Not with Spring AOP. Only beans managed by the Spring container can be proxied and have advice applied to them.

A join point is a specific point in execution, like one particular method call. A pointcut is an expression that selects a set of join points that advice should apply to.

Key Takeaways

  • A cross-cutting concern is behavior needed across many unrelated classes, like logging or security.
  • An aspect encapsulates that behavior in one place, using advice and pointcuts.
  • Spring AOP is proxy-based, so it only intercepts calls that go through the Spring-managed proxy.
  • @EnableAspectJAutoProxy turns on annotation-driven AOP support.
  • Self-invocation within the same class bypasses the proxy and will not trigger advice.

Summary

AOP lets you factor out cross-cutting behavior into a single, reusable aspect instead of duplicating it across your codebase. You have seen @Before advice in action; next you will explore the full family of advice types, including @Around, and build a practical logging aspect that measures method execution time.

Next Lesson →

Spring AOP: Advice Types