Chapter 6: Using aspects with Spring AOP
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
- Purpose of Spring aspects. Explain how aspects add behavior to method calls and why they are useful when the same logic is needed across an app.
- Purpose of separating supporting logic. Explain why an aspect can make a method easier to read and maintain by keeping supporting logic apart from its main business logic, and why aspects must be used carefully.
- Basic aspect terminology. Distinguish the aspect, advice, pointcut, and join point in the terms used by this section.
- Proxy wrapping and weaving. Trace how Spring uses a proxy to apply an aspect when a method on a target bean is called.
- Aspect setup and registration. Describe how the example enables aspects and registers an aspect class: add spring-aspects, use @EnableAspectJAutoProxy on the configuration class, mark the class with @Aspect, and separately make it a Spring bean.
- Advice and pointcut selection. Explain how the example uses @Around on an aspect method and an AspectJ pointcut expression to select calls to intercept. Describe the breadth of the example’s selection.
- Around advice delegation. Trace the example’s around-advice behavior before, during, and after the service method call, including what happens if it does not call proceed(). State how the example treats exceptions from the intercepted method.
- Intercepted call information. Explain how an aspect can access and log an intercepted method’s name, parameters, and returned value.
- Replacement method parameters. Describe how the argument supplied to proceed() determines which parameters reach the intercepted method.
- Caller return value. Distinguish the value returned by the intercepted method from the value the aspect returns to its caller.
- Runtime method annotation setup. Explain why the custom @ToLog annotation needs runtime retention and why its target should be restricted to methods.
- Annotated method pointcut selection. Describe how @annotation(ToLog) selects methods for the existing logging aspect, and what happens when only deleteComment() is marked.
- Advice execution timing. Distinguish when @Before, @AfterReturning, @AfterThrowing, and @After run, including what the latter two receive from the intercepted method.
- Pointcuts and delegation limits. Explain how the alternative advice annotations target methods and why their aspect methods do not control delegation to the intercepted method.
- Multiple Aspect Execution Chain. Trace how two aspects intercepting the same method execute one after another, with each aspect delegating to the next aspect or the intercepted method.
- Consequences of Aspect Order. Explain why putting SecurityAspect before LoggingAspect can prevent logging of calls rejected by security restrictions.
- Explicit Aspect Ordering. Describe when Spring leaves aspect order unspecified and how @Order on aspect classes establishes an execution order.
What it builds on
- Using the @Autowired annotation to inject beans (chapter 3) · The earlier use of dependency injection as a Spring capability supported by IoC.
- Using stereotype annotations to create beans (chapter 2) · Creating Spring beans with stereotype annotations.
Section by section
Introduction
Introduces aspects as a way to add shared behavior to method calls without repeating it in each method.
- Purpose of Spring aspects: Explain how aspects add behavior to method calls and why they are useful when the same logic is needed across an app.
Why it matters: This explains why an app might use aspects instead of putting the same supporting code in many methods.
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.
- Purpose of separating supporting logic: Explain why an aspect can make a method easier to read and maintain by keeping supporting logic apart from its main business logic, and why aspects must be used carefully.
Why it matters: It explains the problem aspects are meant to solve without treating them as an automatic improvement. - Basic aspect terminology: Distinguish the aspect, advice, pointcut, and join point in the terms used by this section.
Why it matters: These terms let you describe what an aspect does and which calls cause it to run. - Proxy wrapping and weaving: Trace how Spring uses a proxy to apply an aspect when a method on a target bean is called.
Why it matters: It explains why calls to an aspected Spring bean pass through another object before reaching the method. - Aspects behind Spring capabilities: Explain why understanding aspects helps when working with Spring capabilities such as transactionality and security configurations.
Why it matters: Aspects are not only a way to organize your own code; they also underpin Spring features you may use.
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.
- Aspect setup and registration: Describe how the example enables aspects and registers an aspect class: add spring-aspects, use @EnableAspectJAutoProxy on the configuration class, mark the class with @Aspect, and separately make it a Spring bean.
Why it matters: Marking a class as an aspect is not enough for Spring to manage and use it. - Advice and pointcut selection: Explain how the example uses @Around on an aspect method and an AspectJ pointcut expression to select calls to intercept. Describe the breadth of the example’s selection.
Why it matters: The pointcut determines which calls receive the aspect’s behavior. - Around advice delegation: Trace the example’s around-advice behavior before, during, and after the service method call, including what happens if it does not call proceed(). State how the example treats exceptions from the intercepted method.
Why it matters: Whether and when the advice calls proceed() determines whether the intercepted service method runs.
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.
- Intercepted call information: Explain how an aspect can access and log an intercepted method’s name, parameters, and returned value.
Why it matters: This lets you explain what the aspect can observe before and after the method runs. - Replacement method parameters: Describe how the argument supplied to proceed() determines which parameters reach the intercepted method.
Why it matters: The arguments at the original call site need not be the arguments the method receives. - Caller return value: Distinguish the value returned by the intercepted method from the value the aspect returns to its caller.
Why it matters: It explains why a caller may observe a result different from the method’s own return value. - Maintainable aspect boundaries: Justify the section’s caution about which logic to put in aspects.
Why it matters: Powerful interception is useful only if the resulting behavior remains understandable to maintainers.
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.
- Runtime method annotation setup: Explain why the custom @ToLog annotation needs runtime retention and why its target should be restricted to methods.
Why it matters: A method marker cannot serve this aspect-selection approach unless it is available at runtime. - Annotated method pointcut selection: Describe how @annotation(ToLog) selects methods for the existing logging aspect, and what happens when only deleteComment() is marked.
Why it matters: The annotation provides a simple way to choose which methods receive the aspect’s logging behavior.
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.
- Advice execution timing: Distinguish when @Before, @AfterReturning, @AfterThrowing, and @After run, including what the latter two receive from the intercepted method.
Why it matters: The event you need to respond to determines which advice annotation fits. - Simpler advice selection: Justify choosing a less powerful advice annotation instead of @Around when it does everything the situation requires.
Why it matters: Using only the flexibility you need keeps aspect code simpler to maintain. - Pointcuts and delegation limits: Explain how the alternative advice annotations target methods and why their aspect methods do not control delegation to the intercepted method.
Why it matters: This distinction tells you what an alternative advice method can—and cannot—control.
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.
- Multiple Aspect Execution Chain: Trace how two aspects intercepting the same method execute one after another, with each aspect delegating to the next aspect or the intercepted method.
Why it matters: Thinking of the aspects as a chain makes their execution order easier to reason about. - Consequences of Aspect Order: Explain why putting SecurityAspect before LoggingAspect can prevent logging of calls rejected by security restrictions.
Why it matters: The right order depends on whether you need logging even when security rejects a call. - Explicit Aspect Ordering: Describe when Spring leaves aspect order unspecified and how @Order on aspect classes establishes an execution order.
Why it matters: Use explicit ordering when the application depends on which aspect intercepts the call first.
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
- Managing exceptions at the endpoint level (chapter 10) · The idea of an aspect intercepting application behavior
- How transactions work in Spring (chapter 13) · The idea that an aspect intercepts method execution.
Concepts explained in this chapter
- interceptorAn aspect is a modular unit in aspect-oriented programming that intercepts selected program operations and applies cross-cutting behavior.
- AspectJ pointcut languageAn AspectJ pointcut expression selects join points, such as method executions, where aspect advice applies.
- proceed() methodThe proceed() method continues an intercepted invocation, optionally supplying replacement arguments.
- ProceedingJoinPointProceedingJoinPoint is a Spring AOP join-point object that exposes the intercepted invocation, including its method, arguments, target object, and continuation of execution.
- advice annotationAn advice annotation is a Spring AOP annotation that specifies when aspect logic runs relative to an intercepted method execution.
- @Aspect annotationThe @Aspect annotation marks a class as defining aspect-oriented behavior for interception by an aspect framework.
- @Around annotationThe @Around annotation marks aspect advice that can run before, after, or instead of an intercepted method and can control whether that method proceeds.
- @Order annotationThe @Order annotation assigns an execution precedence to components whose ordering affects how they are processed.
- aspect-oriented programmingAspect-oriented programming is a programming technique that modularizes behavior applied across multiple program components, such as logging or transaction management.
- adviceAdvice is aspect logic together with the execution timing at which that logic runs relative to an intercepted operation.
- code weavingWeaving is the process of combining aspect advice with target program components at selected execution points.
- pointcutA pointcut is a specification of the methods whose execution is intercepted for aspect application.
- execution chainAn execution chain is an ordered sequence in which multiple aspects and the intercepted method delegate execution to one another.
- execution orderExecution order is the sequence in which multiple aspects run when they intercept the same method.
- proxyA proxy object intercepts calls to a target object and can apply additional behavior before delegating to it.
- @EnableAspectJAutoProxy annotationThe @EnableAspectJAutoProxy annotation enables Spring's automatic proxy creation for AspectJ-style aspects.
- @ServiceThe Spring @Service stereotype annotation marks a class as a service component eligible for detection and registration as a bean.
- loose couplingDecoupling is the reduction of direct dependencies between software components so that one component's implementation can change independently of another's.
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.
