Spring AOP vs AspectJ

Metric Spring AOP AspectJ
Simple/Complex Spring AOP aims to provide a simple AOP implementation across Spring IoC to solve the most common problems that programmers face AspectJ is the original AOP technology which aims to provide complete AOP solution. It is more robust but also significantly more complicated than Spring AOP
Object Scope It can only be applied to beans that are managed by a Spring container. AspectJ can be applied across all domain objects.
Weaving Spring AOP makes use of runtime weaving.

With runtime weaving, the aspects are woven during the execution of the application using proxies of the targeted object – using either JDK dynamic proxy or CGLIB proxy.

  • JDK dynamic proxy – the preferred way for Spring AOP. Whenever the targeted object implements even one interface, then JDK dynamic proxy will be used
  • CGLIB proxy – if the target object doesn’t implement an interface, then CGLIB proxy can be used
AspectJ uses compile time and classload time weaving.

  • Compile-time weaving: The AspectJ compiler takes as input both the source code of our aspect and our application and produces a woven class files as output
  • Post-compile weaving: This is also known as binary weaving. It is used to weave existing class files and JAR files with our aspects
  • Load-time weaving: This is exactly like the former binary weaving, with a difference that weaving is postponed until a class loader loads the class files to the JVM
Performance As far as performance is concerned, compile-time weaving is much faster than runtime weaving. Spring AOP is a proxy-based framework, so there is the creation of proxies at the time of application startup. AspectJ weaves the aspects into the main code before the application executes and thus there’s no additional runtime overhead, unlike Spring AOP.

AspectJ is almost around 8 to 35 times faster than Spring AOP.

Power Less Powerful – only supports method level weaving More Powerful – can weave fields, methods, constructors, static initializers, final class/methods, etc.
Supported Pointcuts Supports only method execution pointcuts Support all pointcuts

Leave a Reply

Your email address will not be published. Required fields are marked *