Spring supports two approaches to AOP: proxy-based AOP and AspectJ-based AOP. Both approaches can be used interchangeably, and Spring provides support for both, allowing developers to choose the most suitable approach for their application needs.
Proxy-based AOP is a runtime AOP solution where a proxy object intercepts the method calls, based on the advice configured, before delegating the call to the target object. This makes AOP possible without requiring modifications to the original codebase. In Spring, this type of AOP is achieved by creating a proxy object around the target object, which implements the same interfaces as the target object, and intercepts the method calls based on the advice configured.
AspectJ-based AOP is a compile-time AOP solution that uses a special syntax to declare and weave cross-cutting concerns into a Java class. With AspectJ, the aspects are defined as regular Java classes with advice and pointcuts. Pointcuts are used to define the set of join points where the advice should be applied. The AspectJ compiler then applies the aspects to the Java class bytecodes during compilation.
To support AspectJ-based AOP, Spring provides both runtime and compile-time weaving options. In runtime weaving, Spring uses an AspectJ weaver to dynamically weave aspects into the target object through a proxy. This approach allows AspectJ-based AOP to be used in an existing Spring application without requiring any modifications to the application or build process.
In contrast, compile-time weaving requires a special build process where AspectJ aspects are compiled and woven into the Java class bytecodes at compile-time. Spring provides a set of Gradle and Maven plugins that can be used to automate the AspectJ weaving process during the build.
In conclusion, Spring supports both proxy-based and AspectJ-based AOP, providing developers with flexibility and options to choose the most appropriate approach for their application needs. Proxy-based AOP is a runtime solution that allows AOP without requiring modifications to the original code, while AspectJ-based AOP is a compile-time solution that allows for more fine-grained control and performance optimization.