Lesson 7 · Spring Core · completes Section 1

Aspect-Oriented Programming

Modularising the concerns that cut across every class — logging, security, transactions — without scattering the code.

You already know the engine Spring AOP is the proxy machinery from Lesson 6: a 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.

The problem AOP solves

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.

The vocabulary (learn these exactly)

TermMeaning
AspectA module of cross-cutting concern = pointcut + advice (a class annotated @Aspect).
Join pointA point in execution where advice can run. In Spring AOP this is always a method execution.
PointcutA predicate that selects join points — the "where".
AdviceThe action taken at a matched join point — the "what".
WeavingLinking aspects to objects. Spring AOP weaves at runtime, via proxies.
TargetThe object being advised (wrapped by the proxy).
Spring AOP is a proxy-based subset of AspectJ Because it's proxy-based, Spring AOP only advises public method executions on Spring beans, weaves at runtime, and — from Lesson 6 — cannot advise final/private/static methods or calls made via this. (self-invocation). Full AspectJ adds field/constructor join points and compile/load-time weaving.

The five advice types

AdviceRuns…Can it…
@Beforebefore the methodprevent execution only by throwing
@AfterReturningafter a normal returnread (not change*) the return value
@AfterThrowingafter the method throwsread the exception — but not stop it propagating
@Afterafter the method, either way ("finally")run cleanup regardless of outcome
@Aroundwraps the methoddo anything: skip the call, change args/return, suppress exceptions

*Only @Around can substitute the return value.

The advice questions the exam loves Suppress a thrown exception? Only @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
  }
}

Pointcut expressions

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)
ExpressionMatches
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.

Primary source — read this
Spring Framework 5.3 — Core: Aspect Oriented Programming with Spring

Read "AOP Concepts", "Declaring Advice", and "Declaring a Pointcut" (the execution examples). You only need @AspectJ-style (annotation) AOP.

Check yourself

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.

AOP drill — 16 questions, on the page

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.

Then, for a second pass The same 16 topics are in your practice book's AOP section — a quick way to see them phrased differently. This is a small, high-yield topic where you can reach 100%.
🏁 Day-5 checkpoint — you've finished Spring Core (the biggest section) Time to gauge the exam-date decision. Re-run the Container Drill cold and do the book's AOP section. Comfortably 80%+? Stay on 15 Aug. Struggling? This is the moment to trigger the ~2-week postponement — tell me and I'll rebalance the plan. Either way, ask me to build a Spring Core mixed review whenever you want one.
I'm your teacher — ask me anything. Want a worked aspect you can run, or more execution(...) patterns to decode? Ask. Say "continue" to start Section 2 — Data Management with Lesson 8 (Spring JDBC).
← Lesson 6 · Bean Lifecycle & Proxies Lesson 8 · Spring JDBC →