Spring AOP是否编译编织时间或加载时间编织?

时间:2014-02-04 10:09:01

标签: java spring aspectj

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

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

我的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>

2 个答案:

答案 0 :(得分:11)

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

在8.1.1项目编织中,它说:

  

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

Spring不像AspectJ那样进行相同类型的加载时编织,但是对代理有效,如文档的另一部分所述:

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

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

答案 1 :(得分:0)

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

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

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

相关问题