Sub topic 1
Why cross-cutting concerns need their own home
Prerequisites
This chapter assumes you know core Java (interfaces, inheritance, annotations) and basic Spring dependency injection (beans, @Component, @Configuration). You should also have a basic idea of what Java reflection is. No AOP knowledge is needed.
The problem: code that cuts across everything
A cross-cutting concern is behavior that many unrelated parts of an application need, but that does not belong to any one of them. Common examples are logging, timing, security checks, auditing, and transaction management.
Consider a service layer where every method must be timed:
import java.util.List;
public class OrderService {
public void placeOrder(String item) {
long start = System.nanoTime();
try {
// business logic: validate item, save order
} finally {
System.out.println("placeOrder took " + (System.nanoTime() - start) + " ns");
}
}
public List<String> listOrders() {
long start = System.nanoTime();
try {
return List.of("book", "pen");
} finally {
System.out.println("listOrders took " + (System.nanoTime() - start) + " ns");
}
}
}The business logic is one or two lines in each method. The timing code surrounds it and is copied into every method. Two symptoms follow from this:
- Scattering: the timing logic is spread across many classes. Changing the output format means editing every copy.
- Tangling: each method now mixes two responsibilities. A reader must separate the business rule from the stopwatch code.
What separating the concern gives you
Aspect-oriented programming (AOP) is a technique that moves a cross-cutting concern into one module and declares where it applies. The business classes go back to containing only business logic:
public class OrderService {
public void placeOrder(String item) {
// business logic only
}
public java.util.List<String> listOrders() {
return java.util.List.of("book", "pen");
}
}The timing code now lives in one place. You change it once, and you can apply it to ten classes or a hundred without touching them.
Alternatives and why they fall short
You can reduce duplication without AOP, but each option has limits.
| Approach | Limit |
|---|---|
| Helper method called from every method | Each method still has to remember to call it. The call sites stay scattered. |
| Inheritance from a base class | Java allows one superclass, so you can inherit only one concern this way. |
| Manual wrapper classes (decorators) | You write one wrapper per interface, and each wrapper repeats the delegation code. |
AOP solves this by generating the wrapper for you from a single rule. In Spring, that wrapper is a proxy object placed around your bean. The next chapters explain the vocabulary and the proxy mechanism in detail.
Concerns you already use
You probably use AOP in Spring without writing an aspect. @Transactional starts and commits a transaction around a method. Method security annotations check permissions before a method runs. Both are cross-cutting concerns applied through proxies, so the business method contains no transaction or permission code.
Exercise
A team copies the same try/catch block that logs exceptions into 40 service methods. Name the two problems this causes and the AOP idea that addresses them.
Show answer
The problems are scattering (the logging logic is spread over 40 places, so one change means 40 edits) and tangling (each method mixes business logic with logging). AOP addresses them by moving the logging into one aspect that is applied to those methods by a declared rule, instead of copied into them.
Exercise
Why is a common base class not a good general solution for cross-cutting concerns?
Show answer
Java classes have a single superclass, so a base class can carry only one concern. It also forces unrelated classes into one hierarchy just to share behavior, and each method must still call the inherited code.