Spring AOP vs AspectJ[英] Spring AOP vs AspectJ

本文是小编为大家收集整理的关于Spring AOP vs AspectJ的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。


我的印象是,Spring AOP最好用于应用程序特定任务,例如安全性,日志记录,交易等.因为它使用自定义的Java5注释作为框架.但是,AspectJ似乎更友好的设计模式.

任何人都可以突出显示在弹簧应用程序中使用Spring AOP vs fackJ的各种利弊吗?


Spring-Aop Pros

  • 使用factexj更简单,因为您不必使用ltw(加载时间编织)或apchs j编译器.

  • 它使用代理图案和装饰器 模式

春季aop cons

  • 这是基于代理的AOP,因此基本上您只能使用方法执行Joinpoints.
  • 在同一类中调用其他方法时不会应用方面.
  • 可能有一些运行时开销.
  • Spring-aop无法为Spring Factory未创建的任何事物添加一个方面

factext j pros

  • 这支持所有联接点.这意味着您可以做任何事情.
  • 开销的开销少于春季AOP.

axpectj cons

  • 请小心.检查您的方面是否仅编织到您想编织的内容.
  • 您需要使用ActectJ编译器或必须设置LTW(加载时间编织)
  • 进行额外的构建过程


附加说明:如果高负载下的性能很重要,那么您将需要比Spring AOP 快9-35倍. 10NS vs 355NS听起来可能并不多,但是我已经看到人们在使用很多方面. 10k的方面价值.在这些情况下,您的要求可能会达到一千个方面.在这种情况下,您将MS添加到该请求中.



除了其他人所说的内容 - 只是为了改写there are two major differences:

  1. 一个与编织类型有关.
  2. Joinpoint定义的另一个.

弹簧aop:使用dynamic proxy if interface exists or cglib library if direct implementation provided.>


expactj:编译时间编织AspectJ Java Tools(ajc compiler)如果可用或发布编译编织(使用编译文件).另外,可以启用使用Spring编织的加载时间 - 它需要aspectj定义文件并提供灵活性.

编译时间编织可以提供性能的好处(在某些情况下)以及joinpoint definition in Spring-aop is restricted to method definition only which is not the case for AspectJ.



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?


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 Decorator pattern

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.