【问题标题】:Spring AOP using AspectJ LTW not working使用 AspectJ LTW 的 Spring AOP 不起作用
【发布时间】:2013-06-09 10:45:30
【问题描述】:

我正在使用 spring aop 并且已经定义了一些方面。我的 tomcat 上启用了 Spring LTW。 在我的应用程序上下文中:

<context:load-time-weaver/>
<aop:aspectj-autoproxy proxy-target-class="false"/>

方面也运行良好!但是目标类被代理了!导致 ClassCastException: can not convert $Proxy... 请注意,我的目标类没有被代理!

【问题讨论】:

    标签: aspectj spring-aop load-time-weaving


    【解决方案1】:

    如果你使用的是 AspectJ LTW,你只需要标签

    <context:load-time-weaver/> 
    

    在您的 Spring 上下文文件中。所以你可以删除,

    <aop:aspectj-autoproxy proxy-target-class="false"/>
    

    如果目标类被代理是因为 AspectJ 的 LTW 配置不正确,因此不是 AspectJ 正在处理您的建议,而是 Spring 正在这样做。因此,您会看到基于代理的目标类。

    检查此链接,

    http://static.springsource.org/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw

    http://static.springsource.org/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw-environments

    【讨论】:

    • 那么,你是说如果我使用带有LTW的spring AOP,就没有办法不代理目标类吗? (我不想使用 AspectJ 编译器!)
    • 如果你想要 LTW,你必须使用 AspectJ 编译器。更重要的是,如果您想要 LTM,您将不得不使用 META-INF/aop.xml 文件。 Spring 本身不能使用 LTW,只能创建基于代理的方面。如果你想要 LTW,你需要引入 AspectJ。您可以将 ApectJ 与 Spring 集成(甚至是 AspectJ 建议中的依赖注入),但是如果没有 AspectJ 并且仅使用 Spring,您就不能拥有 LTW。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2012-01-15
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多