views:

1276

answers:

3

I am under the impression that Spring-AOP is best used for application specific tasks such as security, logging, transactions, etc. as it uses custom Java5 annotations as a framework. However, AspectJ seems to be more friendly design-patterns wise.

Can anyone highlight the various pros and cons of using Spring-AOP vs AspectJ in a spring application??

+6  A: 

Spring-AOP Pros

  • This is more simple to use then AspectJ, because it doesn't have to use LTW(load-time weaving) or AspectJ compiler.
  • This can be change to AspectJ AOP
    when you use @Aspect annotation based Spring AOP.
  • This use Proxy pattern and Decorator pattern

Spring-AOP Cons

  • This is proxy-based AOP. So basically you can only use method-execution pointcut.
  • There can be a little runtime overhead.

AspectJ Pros

  • This supports all pointcuts. This means you can do anything.
  • There is little runtime overhead.

AspectJ Cons

  • Be careful. Check if your aspects are weaved to only what you wanted to be weaved.
  • You need extra build process with ApsectJ Compiler or have to setup LWT(Load-Time Weating)
Whiteship
One of the most essential points isn't directly addressed here. Spring-AOP cannot add an aspect to anything that is not created by the Spring factory.
Jherico
+1  A: 

The spring user manual will give a lot of information, straight from the horse's mouth.

The chapter 6.4 - Choosing which AOP declaration style to use is dead on for you since it discusses the pros and cons of both.

The paragraph 6.1.2 - Spring AOP Capabilites and goals & chapters 6.2 - @Aspect support and 6.8 - Using AspectJ with Spring applications should be particularily interesting.

elhoim
+1  A: 

Apart from what others have stated - just to rephrase, there are two major differences. One is related to the type of weaving and 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.

techzen