Spring AOP vs AspectJ Spring AOP vs AspectJ java java

Spring AOP vs AspectJ


Spring-AOP Pros

  • It is simpler to use than AspectJ, since you don't have to use LTW (load-time weaving) or the AspectJ compiler.

  • It uses the Proxy pattern and the Decoratorpattern

Spring-AOP Cons

  • This is proxy-based AOP, so basically you can only use method-execution joinpoints.
  • Aspects aren't applied when calling another method within the same class.
  • There can be a little runtime overhead.
  • Spring-AOP cannot add an aspect to anything that is not created by the Spring factory

AspectJ Pros

  • This supports all joinpoints. This means you can do anything.
  • There is less runtime overhead than that of Spring AOP.

AspectJ Cons

  • Be careful. Check if your aspects are weaved to only what you wanted to be weaved.
  • You need extra build process with AspectJ Compiler or have to setup LTW (load-time weaving)


An additional note: If performance under high load is important, you'll want AspectJ which is 9-35x faster than Spring AOP. 10ns vs 355ns might not sound like much, but I've seen people using LOTS of Aspects. 10K's worth of aspects. In these cases, your request might hit a thousands of aspects. In that case you're adding ms to that request.

See the benchmarks.


Apart from what others have stated - just to rephrase, there are two major differences:

  1. One is related to the type of weaving.
  2. Another to the joinpoint definition.

Spring-AOP: Runtime weaving through proxy using concept of dynamic proxy if interface exists or cglib library if direct implementation provided.

AspectJ: Compile time weaving through AspectJ Java Tools(ajc compiler) if source available or post compilation weaving (using compiled files). Also, load time weaving with Spring can be enabled - it needs the aspectj definition file and offers flexibility.

Compile time weaving can offer benefits of performance (in some cases) and also the joinpoint definition in Spring-aop is restricted to method definition only which is not the case for AspectJ.