Skip to main content

Overview

| Adapted from https://docs.spring.io/spring-framework/docs/4.0.x/spring-framework-reference/html/aop.html

Concepts

ConceptDescription
AspectModularization of a cross-cutting concern e.g. Transaction management
Join PointA point during the execution of a program
AdviceAction taken by aspect at a join point
PointcutPredicate that matches joinpoints
IntroductionInterfaces for Advices
Target ObjectObject being advised by one or more object
AOP ProxyObject created by AOP framework to implement contracts (JDK Dynamic Proxy / CGLib Proxy in Spring)
WeavingLinking aspects with other application types to create advised object

Advice

AdviceWhen it is executed
BeforeBefore a JP (cannot stop execution unless error thrown)
After ReturnAfter JP completes normally
After ThrowingAfter method exits by throwing an exception
After (finally)After method exits (regardless of how)
AroundAdvice that surrounds a joinpoint during execution
note

Recommended to use the least powerful advice required for implementing an aspect => simpler model will have less potential for errors

AOP Proxies

  • (Default) Standard JDK dynamic proxies - for interface
  • CGLib Proxies - for classes
  • Further Reading
note

It is good practise to use interfaces rather than classes as business class normally implements one or more interfaces


@AspectJ Support

Enabling AspectJ Support

@Configuration
@EnableAspectJAutoProxy
public class AppConfig{}

Declaring an Aspect

  • Aspects cannot target other aspects
  • Register Aspect as a bean with @Component
@Aspect
public class someAspect {
}
warning

Not sufficient to just use @Aspect for component scanning, also need @Component

Not possible to target Aspects of advise from other aspects