简体   繁体   English

Spring AOP 与 AspectJ

[英]Spring AOP vs 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.我的印象是 Spring AOP 最适合用于特定于应用程序的任务,例如安全性、日志记录、事务等,因为它使用自定义 Java5 注释作为框架。 However, AspectJ seems to be more friendly design-patterns wise.然而,AspectJ 似乎更友好的设计模式明智。

Can anyone highlight the various pros and cons of using Spring AOP vs AspectJ in a Spring application?任何人都可以强调在 Spring 应用程序中使用 Spring AOP 与 AspectJ 的各种优缺点吗?

Spring-AOP Pros Spring-AOP 优点

  • It is simpler to use than AspectJ, since you don't have to use LTW ( load-time weaving ) or the AspectJ compiler.它比 AspectJ 更易于使用,因为您不必使用 LTW(加载时编织)或 AspectJ 编译器。

  • It uses the Proxy pattern and the Decorator pattern它使用代理模式和装饰模式

Spring-AOP Cons Spring-AOP 的缺点

  • This is proxy-based AOP, so basically you can only use method-execution joinpoints.这是基于代理的 AOP,所以基本上你只能使用方法执行连接点。
  • 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 Spring-AOP 不能向不是由 Spring 工厂创建的任何内容添加方面

AspectJ Pros AspectJ 优点

  • This supports all joinpoints.这支持所有连接点。 This means you can do anything.这意味着你可以做任何事情。
  • There is less runtime overhead than that of Spring AOP.运行时开销比 Spring AOP 少。

AspectJ Cons AspectJ 缺点

  • 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)您需要使用 AspectJ Compiler 进行额外的构建过程,或者必须设置 LTW(加载时编织)

Apart from what others have stated - just to rephrase, there are two major differences :除了其他人所说的 - 只是改写一下, 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. Spring-AOP: dynamic proxy if interface exists or cglib library if direct implementation provided.使用dynamic proxy if interface exists or cglib library if direct implementation provided.概念dynamic proxy if interface exists or cglib library if direct implementation provided.通过代理进行运行时编织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). AspectJ:如果源代码可用,则通过AspectJ Java Tools(ajc compiler)编译时编织或编译后编织(使用编译文件)。 Also, load time weaving with Spring can be enabled - it needs the aspectj definition file and offers flexibility.此外,可以启用 Spring 的加载时间编织 - 它需要aspectj定义文件并提供灵活性。

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.编译时编织可以提供性能优势(在某些情况下),并且joinpoint definition in Spring-aop is restricted to method definition only which is not the case for AspectJ.joinpoint definition in Spring-aop is restricted to method definition only which is not the case for AspectJ.

An additional note: If performance under high load is important, you'll want AspectJ which is 9-35x faster than Spring AOP .附加说明:如果高负载下的性能很重要,您将需要比 Spring AOP 快 9-35 倍的 AspectJ 10ns vs 355ns might not sound like much, but I've seen people using LOTS of Aspects. 10ns vs 355ns 听起来可能不多,但我见过人们使用很多方面。 10K's worth of aspects. 10K的价值方面。 In these cases, your request might hit a thousands of aspects.在这些情况下,您的请求可能涉及数千个方面。 In that case you're adding ms to that request.在这种情况下,您将 ms 添加到该请求中。

See the benchmarks .请参阅基准

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.6.4- 选择要使用的 AOP 声明样式对您来说已经死了,因为它讨论了两者的优缺点。

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.6.1.2- Spring AOP 功能和目标和章节6.2 - @Aspect 支持6.8 - 在 Spring 应用程序中使用 AspectJ应该特别有趣。

Spring AOP is one of the essential parts of the spring framework. Spring AOP 是 Spring 框架的重要组成部分之一。 At the very basic stage, the spring framework is based on IoC and AOP.在非常基础的阶段,spring 框架是基于 IoC 和 AOP 的。 In the official course of Spring there is a slide in which it says:在 Spring 的官方课程中有一张幻灯片,其中说:

The AOP is one of the most important parts of the framework. AOP 是框架中最重要的部分之一。

The key point for understanding how AOP in Spring works is that when you write an Aspect with Spring we instrument the framework with building a proxy for your objects, with a JDKDynamicProxy if your bean implements an interface or via CGLIB if your bean doesn't implement any interface.理解 Spring 中 AOP 如何工作的关键点是,当您使用 Spring 编写 Aspect 时,我们通过为您的对象构建代理来检测框架,如果您的 bean 实现了一个接口,则使用JDKDynamicProxy或如果您的 bean 没有实现,则通过 CGLIB任何接口。 Remember that you must have cglib 2.2 in your class-path if you're using Spring prior to version 3.2.请记住,如果您使用 3.2 版之前的 Spring,则您的类路径中必须有 cglib 2.2。 Starting from Spring 3.2 it is useless because cglib 2.2 was included in the core.从 Spring 3.2 开始它是无用的,因为 cglib 2.2 包含在核心中。

The framework at the bean creation will create a proxy that wraps your objects and adds cross cutting concerns responsibilities such as security, transaction management, logging and so on. bean 创建时的框架将创建一个代理来包装您的对象并添加横切关注点职责,例如安全性、事务管理、日志记录等。

The proxy creation in this way will be applied starting for a pointcut expression that instruments the framework to decide what beans and methods will be created as proxies.以这种方式创建的代理将从切入点表达式开始应用,该切入点表达式检测框架以决定将创建哪些 bean 和方法作为代理。 The advice will be the more responsibility than for your code.建议将比您的代码承担更多的责任。 Remember that in this process the pointcut captures only public methods that aren't declared as final.请记住,在此过程中,切入点仅捕获未声明为 final 的公共方法。

Now, while in Spring AOP the weaving of Aspects will be performed by the container at container start-up, in AspectJ you have to perform this with a post compilation of your code through bytecode modification.现在,虽然在 Spring AOP 中,Aspects 的编织将由容器在容器启动时执行,但在 AspectJ 中,您必须通过字节码修改对代码进行后期编译来执行此操作。 For this reason in my opinion the Spring approach is simpler and more manageable than AspectJ.出于这个原因,我认为 Spring 方法比 AspectJ 更简单、更易于管理。

On the other hand, with the Spring AOP you can't use the all power of AOP because the implementation is done through proxies and not with through modification of your code.另一方面,使用 Spring AOP,您不能使用 AOP 的所有功能,因为实现是通过代理完成的,而不是通过修改代码来完成的。

As in AspectJ, you can use load-time weaving in SpringAOP.与在 AspectJ 中一样,您可以在 SpringAOP 中使用加载时编织。 You can benefit from this feature in spring is implemented with an agent and special configurations, @EnabledLoadWeaving or in XML.您可以从 spring 中的此功能中受益,该功能是通过代理和特殊配置、 @EnabledLoadWeaving或在 XML 中实现的。 You can use the name-space as an example.您可以使用名称空间作为示例。 However in Spring AOP you can't intercept all the cases.但是在 Spring AOP 中,您无法拦截所有情况。 For example, the new command isn't supported in Spring AOP.例如,Spring AOP 不支持new命令。

However in Spring AOP you can benefit from the usage of AspectJ through the use of the aspectof factory method in the spring configuration bean.但是,在 Spring AOP 中,您可以通过在 spring 配置 bean 中使用aspectof工厂方法,从 AspectJ 的使用中aspectof

For the reason that Spring AOP is basically a proxy created from the container, so you can use AOP only for spring beans.因为 Spring AOP 基本上是从容器创建的代理,所以只能对 spring bean 使用 AOP。 While with AspectJ you can use the aspect in all your beans.使用 AspectJ 时,您可以在所有 bean 中使用切面。 Another point of comparison is debug and the predictability of the code behavior.另一个比较点是调试和代码行为的可预测性。 With spring AOP, the job is preformed all from the Java compiler and aspects are a very cool way for creating proxy for your Spring bean.使用 spring AOP,这项工作全部由 Java 编译器执行,方面是为 Spring bean 创建代理的一种非常酷的方式。 In AspectJ if you modify the code, you need more compiling and to understand where your aspects are woven could be hard.在 AspectJ 中,如果您修改代码,则需要更多的编译,并且了解方面的编织位置可能会很困难。 Even shutting down the weaving in spring is simpler: with spring you remove the aspect from your configuration, restart and it works.即使在 spring 中关闭编织也更简单:使用 spring,您可以从配置中删除方面,重新启动,它就可以工作了。 In AspectJ you must recompile the code!在 AspectJ 中,您必须重新编译代码!

In load-time weaving, AspectJ is more flexible than Spring because Spring doesn't support all of the options of AspectJ.在加载时编织方面,AspectJ 比 Spring 更灵活,因为 Spring 并不支持 AspectJ 的所有选项。 But in my opinion If you want to change the creation process of a bean, a better way is to manage the custom login in a factory and not with load-time weaving of an aspect that changes the behavior of your new operator.但在我看来,如果您想更改 bean 的创建过程,更好的方法是在工厂中管理自定义登录,而不是通过加载时编织改变新操作符行为的方面。

I hope that this panoramic of AspectJ and Spring AOP helps you understand the difference of the two potions希望这张AspectJ和Spring AOP的全景图能帮助大家了解这两种药水的区别

This article also has a good explanation regarding the topic.这篇文章也对这个话题有很好的解释。

Spring AOP and AspectJ have different goals. Spring AOP 和 AspectJ 有不同的目标。

Spring AOP aims to provide a simple AOP implementation across Spring IoC to solve the most common problems that programmers face. Spring AOP 旨在提供跨 Spring IoC 的简单 AOP 实现,以解决程序员面临的最常见问题。

On the other hand, AspectJ is the original AOP technology which aims to provide complete AOP solution.另一方面,AspectJ 是原始的 AOP 技术,旨在提供完整的 AOP 解决方案。

It is important to consider whether your aspects will be mission critical and where your code is being deployed.重要的是要考虑您的方面是否将是关键任务以及您的代码部署在哪里。 Spring AOP will mean that you are relying on load-time weaving. Spring AOP 将意味着您依赖加载时编织。 This can fail to weave and in my experience has meant that logged errors may exist but will not prevent the application from running without aspect code [I would add the caveat that it may be possible to configure it in such a way that this is not the case;这可能会失败编织在我的经验意味着,可能存在记录错误,但不会阻止应用程序而无需方面运行的代码[我要补充的警告,它可能配置它以这样的方式,这是不案件; but I am not personally aware of it ].但我个人并不知道]。 Compile-time weaving avoids this.编译时编织避免了这种情况。

Additionally, If you use AspectJ in conjunction with the aspectj-maven-plugin then you are able to run unit tests against your aspects in a CI environment and have confidence that built artifacts are tested and correctly woven.此外,如果您将 AspectJ 与 aspectj-maven-plugin 结合使用,那么您就能够在 CI 环境中针对您的方面运行单元测试,并确信构建的工件已经过测试和正确编织。 While you can certainly write Spring driven unit tests, you still have no guarantee that the deployed code will be that which was tested if LTW fails.虽然您当然可以编写 Spring 驱动的单元测试,但您仍然无法保证部署的代码将是在 LTW 失败时测试过的代码。

Another consideration is whether you are hosting the application in an environment where you are able to directly monitor the success or failure of a server / application startup or whether your application is being deployed in an environment where it is not under your supervision [eg where it is hosted by a client].另一个考虑因素是您是否将应用程序托管在能够直接监控服务器/应用程序启动成功或失败的环境中,或者您的应用程序是否部署在不受您监督的环境中[例如由客户托管]。 Again, this would point the way to compile time weaving.同样,这将指出编译时编织的方法。

Five years ago, I was much more in favour of Spring configured AOP for the simple reason that it was easier to work with and less likely to chew up my IDE.五年前,我更喜欢 Spring 配置的 AOP,原因很简单,它更容易使用,而且不太可能占用我的 IDE。 However, as computing power and available memory have increased this has become much less of an issue and CTW with the aspectj-maven-plugin has become a better choice in my work environment based on the reasons I have outlined above.然而,随着计算能力和可用内存的增加,这已经变得不那么重要了,基于我上面概述的原因,带有 aspectj-maven-plugin 的 CTW 已成为我工作环境中更好的选择。

Compared to AOP, AspectJ does not need to enhance the target class at compile time.与 AOP 相比,AspectJ 不需要在编译时增强目标类。 Instead, it generates a proxy class for the target class at runtime, which either implements the same interface as the target class or is a subclass of the target class.相反,它在运行时为目标类生成一个代理类,它要么实现与目标类相同的接口,要么是目标类的子类。

In summary, an instance of a proxy class can be used as an instance of a target class.总之,代理类的实例可以用作目标类的实例。 In general, the compile-time enhanced AOP framework is more advantageous in performance—because the runtime-enhanced AOP framework requires dynamic enhancements every time it runs.总的来说,编译时增强型AOP框架在性能上更有优势——因为运行时增强型AOP框架每次运行都需要动态增强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM