Modularising the concerns that cut across every class — logging, security, transactions — without scattering the code.
BeanPostProcessor wraps your bean in a
proxy that runs extra code around your methods. This lesson names the parts and the pointcut syntax. Expect
questions on terminology, the five advice types, and reading an execution(...) expression.
A cross-cutting concern is behaviour needed in many places but belonging to none of them — logging, security checks, transactions, caching, performance timing. Without AOP that code gets copy-pasted into every method. AOP lets you write it once as an aspect and declare where it applies.
AOP complements IoC: IoC wires objects together; AOP adds behaviour across them.
| Term | Meaning |
|---|---|
| Aspect | A module of cross-cutting concern = pointcut + advice (a class annotated @Aspect). |
| Join point | A point in execution where advice can run. In Spring AOP this is always a method execution. |
| Pointcut | A predicate that selects join points — the "where". |
| Advice | The action taken at a matched join point — the "what". |
| Weaving | Linking aspects to objects. Spring AOP weaves at runtime, via proxies. |
| Target | The object being advised (wrapped by the proxy). |
final/private/static
methods or calls made via this. (self-invocation). Full AspectJ adds field/constructor join points
and compile/load-time weaving.
| Advice | Runs… | Can it… |
|---|---|---|
@Before | before the method | prevent execution only by throwing |
@AfterReturning | after a normal return | read (not change*) the return value |
@AfterThrowing | after the method throws | read the exception — but not stop it propagating |
@After | after the method, either way ("finally") | run cleanup regardless of outcome |
@Around | wraps the method | do anything: skip the call, change args/return, suppress exceptions |
*Only @Around can substitute the return value.
@Around (catch it and don't rethrow). Run whether or
not the method throws? @After. Decide if the target runs at all / change the return?
@Around, by controlling proceed(). @AfterThrowing can see the
exception but cannot swallow it.
@Around in code@Aspect @Component
public class TimingAspect {
@Around("execution(* com.app.service.*.*(..))")
public Object time(ProceedingJoinPoint pjp) throws Throwable {
long t = System.nanoTime();
Object result = pjp.proceed(); // run the target — omit this to skip it
log(System.nanoTime() - t);
return result; // could return something else instead
}
}
Spring uses the AspectJ pointcut language. execution(...) is the one to know:
execution( * com.app.service.*.*(..) )
│ │ │ │
return type │ method args (.. = any number)
package.type (* = any type directly in the package)
| Expression | Matches |
|---|---|
execution(* transfer(..)) | any transfer method, any args, any return type |
execution(* com.app.service.*.*(..)) | every method of every type directly in com.app.service |
execution(public * *(..)) | any public method (only execution can match on access modifier) |
within(com.app.service.*) | any join point within types in that package |
Combine pointcuts with &&, ||, and !. Note ..
means "any number of" — arguments inside (), or sub-packages inside a type pattern.
Read "AOP Concepts", "Declaring Advice", and "Declaring a Pointcut" (the execution examples). You only need @AspectJ-style (annotation) AOP.
The advice-type questions (which one suppresses an exception / runs either way) and reading an execution expression are the money questions. Options shuffle on every load.
Covers the same ground as your practice book's AOP section — terminology, advice types, and pointcut expressions — so you can drill it here without opening the book. Options and order shuffle on every load.
execution(...) patterns to decode? Ask. Say
"continue" to start Section 2 — Data Management with Lesson 8 (Spring JDBC).