带有 AspectJ 的 Spring AOP:加载时间编织
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26325732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring AOP with AspectJ: Load time weaving
提问by Shane
If I'm using AspectJ based Spring AOP, am I then tied to configuring my aspects to use load time weaving? Or does Spring AOP also support run time/compile time weaving when using the AspectJ based approach?
如果我正在使用基于 AspectJ 的 Spring AOP,那么我是否会绑定到配置方面以使用加载时间编织?或者在使用基于 AspectJ 的方法时,Spring AOP 是否也支持运行时/编译时编织?
采纳答案by Angad
Spring AOP is proxy based. Unless configured to do otherwise, Spring AOP performs run-time weaving.
Spring AOP 是基于代理的。除非配置为其他方式,否则 Spring AOP 将执行运行时编织。
Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
编织:将方面与其他应用程序类型或对象联系起来,以创建建议对象。这可以在编译时(例如,使用 AspectJ 编译器)、加载时或运行时完成。Spring AOP 与其他纯 Java AOP 框架一样,在运行时执行编织。
来源:http: //docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#aop-introduction-defn
You can however set up Spring to do load-time weaving. Check Spring documentation on how to do this: http://docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw
但是,您可以设置 Spring 来进行加载时编织。查看有关如何执行此操作的 Spring 文档:http: //docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw
Among other things, you would be using @EnableLoadTimeWeavingin your Java Config class.
The setup is fairly simple and your @Aspectclasses would not change.
除其他外,您将@EnableLoadTimeWeaving在 Java Config 类中使用。设置相当简单,您的@Aspect课程不会改变。
Developers simply modify one or more files that form the application context to enable load-time weaving instead of relying on administrators who typically are in charge of the deployment configuration such as the launch script
开发人员只需修改构成应用程序上下文的一个或多个文件即可启用加载时编织,而不是依赖通常负责部署配置(例如启动脚本)的管理员
回答by kriegaex
I think we should be careful not to mix up Spring AOP vs. AspectJ.
我认为我们应该小心不要混淆 Spring AOP 和 AspectJ。
- Like singh101 said, Spring AOP is proxy-based, more exactly based on Java SE dynamic proxies (for interfaces) or CGLIB proxies (for classes). It uses a subset of AspectJ syntax and is a kind of "AOP lite" approach basically limited to method execution pointcuts, missing many AspectJ pointcut types like method call, class member set/get, constructor call/execution and others. Technologically it is very much different from AspectJ and always incurs a runtime overhead due to the proxy approach (call indirection). Furthermore, it is limited to Spring Bean methods being called from outside the bean class, i.e. it does not work if a bean calls one of its own methods (because it does not go through the corresponding proxy) and it also does not work for non-Spring Bean classes (normal POJOs).
- AspectJ on the other hand is a full-fledged AOP framework which does not rely on either proxies or the Spring framework. It can be easily included into Spring applications, though. It works by generating byte code directly via its own compiler (which is a superset of the Java compiler) or instrumenting existing byte code. AspectJ can be used during compile time (no runtime overhead) or during classloading (load time weaving, LTW). While LTW has a little overhead during application start-up time (but the same applies to Spring AOP), both AspectJ weaving approaches have no runtime overhead due to call indirection because there are no proxies involved.
- The Spring manual chapter on AOP explains nicely how to integrate full AspectJ into Springwhen Spring AOP is not powerful enough or simply too slow.
- 就像 singh101 所说,Spring AOP 是基于代理的,更准确地说是基于 Java SE 动态代理(用于接口)或 CGLIB 代理(用于类)。它使用 AspectJ 语法的一个子集,是一种“AOP lite”方法,基本上仅限于方法执行切入点,缺少许多 AspectJ 切入点类型,如方法调用、类成员集/获取、构造函数调用/执行等。从技术上讲,它与 AspectJ 有很大不同,并且由于代理方法(调用间接)总是会产生运行时开销。此外,它仅限于从 bean 类外部调用 Spring Bean 方法,即如果 bean 调用它自己的方法之一(因为它没有通过相应的代理),它也不起作用,并且它也不适用于非-Spring Bean 类(普通 POJO)。
- 另一方面,AspectJ 是一个成熟的 AOP 框架,它不依赖于代理或 Spring 框架。不过,它可以很容易地包含在 Spring 应用程序中。它的工作原理是通过自己的编译器(它是 Java 编译器的超集)直接生成字节码或检测现有的字节码。AspectJ 可以在编译时(无运行时开销)或类加载(加载时编织,LTW)期间使用。虽然 LTW 在应用程序启动期间有一点开销(但同样适用于 Spring AOP),但由于不涉及代理,因此两种 AspectJ 编织方法都没有由于调用间接导致的运行时开销。
- 关于 AOP 的 Spring 手册章节很好地解释了当 Spring AOP 不够强大或太慢时如何将完整的 AspectJ 集成到 Spring 中。

