liveBook · Manning

Chapter 6: Using aspects with Spring AOP

What you will learn in chapter 6 of Spring Start Here, Second Edition

Introduces aspects as a way to add shared behavior to method calls without repeating it in each method.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces aspects as a way to add shared behavior to method calls without repeating it in each method.

Key terms: aspects, intercept method calls, extra behavior

6.1 How aspects work in Spring

This section explains why aspects separate supporting logic from business code, defines the basic Spring AOP terms, and shows how a proxy applies an aspect to a target bean.

Key terms: supporting logic, business logic, aspect-oriented programming (AOP), aspect, advice, pointcut, join point, target object, Spring context, proxy object

6.2 Implementing aspects with Spring AOP

Introduces a scenario in which an application must log when each use case begins and ends, setting up the aspect implementations that follow.

6.2.1 Implementing a simple aspect

This section builds a simple Spring aspect that logs before and after a service method call, explaining how to register it, select calls to intercept, and delegate to the method.

Key terms: spring-aspects, @EnableAspectJAutoProxy, @Aspect, Spring context, bean, @Around, advice annotation, AspectJ pointcut expression, services package, ProceedingJoinPoint

6.2.2 Altering the intercepted method’s parameters and the returned value

This section shows how an aspect can inspect an intercepted call, change the arguments passed to its method, and return a different value to the caller, while warning against hiding important logic.

Key terms: ProceedingJoinPoint, intercepted method, parameters, returned value, proceed(), original parameters, array of objects, caller, aspects, decoupling

6.2.3 Intercepting annotated methods

This section shows how a custom annotation and an AspectJ pointcut let a Spring aspect log only marked methods.

Key terms: @ToLog, @Retention(RetentionPolicy.RUNTIME), @Target, @annotation(ToLog), AspectJ pointcut expression, deleteComment()

6.2.4 Other advice annotations you can use

This section compares four simpler Spring AOP advice annotations with @Around, explaining when each runs and how their control differs.

Key terms: @Before, @AfterReturning, @AfterThrowing, @After, intercepted method, @Around, advice annotations, AspectJ pointcut expression, ProceedingJoinPoint

6.3 The aspect execution chain

This section explains how multiple aspects form an execution chain, why their order can change behavior, and how to specify that order with @Order.

Key terms: aspect, execution chain, intercepted method, SecurityAspect, LoggingAspect, execution order, @Order, ordinal

6.4 Questions

This section lists questions for reviewing Spring AOP concepts covered earlier in the chapter, without explaining their answers.

6.5 Summary

This summary recaps what Spring aspects do, why to use them selectively, how to define and target them, and how to order multiple aspects.

Where this is used later

Concepts explained in this chapter

← Chapter 5: The Spring context: Bean scopes and life cycle Chapter 7: Understanding Spring Boot and Spring →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.