Java Spring AOP 做编译时编织还是加载时编织?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21549371/
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
Does Spring AOP do compile time weaving or load time weaving?
提问by DaveJohnston
I am starting to use Spring AOP for a project and I am a little bit confused about weaving. I know that Spring AOP has a dependency on AspectJweaver.jar, but as the documentation says, this is not for the weaving, but just that it uses some of the classes from this jar.
我开始在一个项目中使用 Spring AOP,我对编织有点困惑。我知道 Spring AOP 依赖于 AspectJweaver.jar,但正如文档所说,这不是用于编织,而只是它使用了这个 jar 中的一些类。
But my question is, if it is not using AspectJ for weaving, does Spring AOP have its own weaving and is it performed at load time or compile time?
但我的问题是,如果它没有使用 AspectJ 进行编织,那么 Spring AOP 是否有自己的编织,它是在加载时执行还是在编译时执行?
The relevant part of my Spring Configuration XML file is:
我的 Spring 配置 XML 文件的相关部分是:
<context:annotation-config />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="myaop" expression="execution(* my.package.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="myaop" />
</aop:config>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
采纳答案by sheltem
Under 8.1.1, item weaving, it says:
在 8.1.1,物品编织下,它说:
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 框架一样,在运行时执行编织。
Spring doesn't do the same type of load-time weaving as AspectJ, but works on proxies, as described in another part of the documentation:
Spring 不执行与 AspectJ 相同类型的加载时编织,而是在代理上工作,如文档的另一部分所述:
Edit: Just saw your comment, you are correct in that assumption. The documentation gives a rather complete explanation of how it works exactly. :)
编辑:刚刚看到您的评论,您的假设是正确的。该文档对它的工作原理进行了相当完整的解释。:)
回答by skryvets
An update who is reading this in 2019 and probably later:
正在 2019 年及以后阅读本文的更新:
In spring 5.x aspectjweaver.jar
was removed as a dependency and there is a need to include it separately if you want to use @AspectJ style annotations (or use Spring Boot for example).
在 spring 5.xaspectjweaver.jar
中作为依赖项被删除,如果您想使用 @AspectJ 样式注释(或例如使用 Spring Boot),则需要单独包含它。
- Spring 5.x POM(doesn't include)
- Spring 4.x POM(does include)
- Spring 5.x POM(不包括)
- Spring 4.x POM(包括)
Weaving principles stay the same though - documentation
编织原则保持不变 -文档