【问题标题】:Does Spring AOP do compile time weaving or load time weaving?Spring AOP 是编译时编织还是加载时编织?
【发布时间】:2014-02-04 10:09:01
【问题描述】:

我开始在一个项目中使用 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>

【问题讨论】:

  • 很确定我现在已经弄清楚了,但很高兴听到回复确认。 Spring 也不这样做,它使用将在运行时创建的代理。听起来对吗?

标签: java spring aspectj


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    编织原则保持不变 - documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多