Spring AOP是编译时间编织还是加载时间编织?

问题描述 投票:7回答:2

我开始将Spring AOP用于一个项目,对于编织我有些困惑。我知道Spring AOP依赖于AspectJweaver.jar,但是正如文档所说,这不是用于编织,而只是它使用了此jar中的某些类。

但是我的问题是,如果不使用AspectJ进行编织,Spring AOP是否具有自己的编织,它是在加载时还是在编译时执行?

我的Spring Configuration 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>
java spring aspectj
2个回答
13
投票

http://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#aop-introduction-defn

在8.1.1之下,物品编织说:

编织:将方面与其他应用程序类型或对象链接到创建一个建议对象。这可以在编译时完成(使用例如,AspectJ编译器),加载时间或运行时。春季AOP,像其他纯Java AOP框架一样,在运行时执行编织。

Spring不执行与AspectJ相同类型的加载时编织,但是可以在代理上工作,如文档的另一部分所述:

http://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#aop-understanding-aop-proxies

编辑:刚刚看到您的评论,您的假设是正确的。该文档对它如何工作提供了相当完整的解释。 :)


0
投票

谁正在2019年及以后阅读此内容的更新:

在spring 5.x中aspectjweaver.jar被删除为依赖项,如果您想使用@AspectJ样式注释(或例如使用Spring Boot,则需要单独包含它。

尽管编织原理保持不变-documentation

© www.soinside.com 2019 - 2024. All rights reserved.