【问题标题】:Eclipse: How to convert a web project into an AspectJ project and weave and run it using the AJDT plug in?Eclipse:如何将 Web 项目转换为 AspectJ 项目并使用 AJDT 插件进行编织和运行?
【发布时间】:2010-11-01 07:33:57
【问题描述】:

我想做的事:

  • 我想在 Spring 中使用 @Configured 注解。它需要启用 AspectJ。我认为使用 AJDT 插件进行编译时编织可以解决这个问题。在安装插件之前,应该注入我的 @Configured 对象的依赖项仍然为空。

我做了什么:

  • 为 Eclipse 3.4 安装了 AJDT: AspectJ Development Tools 插件。
  • 右键单击我的 Web 项目并将其转换为 AspectJ 项目。
  • 启用编译时编织。

什么不起作用:

  • 当我现在启动 Tomcat 6 服务器时,出现异常*。

其他信息:

  • 我没有在项目属性的 AspectJ Build 和 AspectJ Compiler 部分中配置任何内容。
  • Preferences 下的 JDT Weaving 显示编织已启用。
  • 我仍然在项目属性下有 Java 构建路径和 Java 编译器。而且它们看起来就像我之前配置的一样(而上面的两个新条目没有配置)。
  • 我的@Configured 目标文件的图标看起来像任何其他文件(即没有任何方面的指示,我认为应该有)。文件名是 MailNotification.java(而不是 .aj),但我想它应该仍然可以工作,因为我正在为 AspectJ 使用 Spring 注释?
  • 我还没有找到任何教程或类似的教程:如何将 Spring Web 应用程序项目转变为 AspectJ 项目并使用 AJDT 插件将方面编织到文件中,所有这些都在 Eclipse 3.4 中。如果有类似的东西,我会非常有兴趣了解它。

我想知道的:

  • 从这里到哪里去?我只想使用Spring的@Configured注解。我也在使用@Transactional,我认为它也需要 AspectJ。
  • 如果可能的话,只要满足我的需求,我想尽可能少地学习 AspectJ。这个主题看起来很有趣,但是很大,我想做的就是使用上面提到的两个 Spring 注释。

*** Tomcat 6 启动时的异常:

Caused by: java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar
at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:82)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 41 more

【问题讨论】:

    标签: java eclipse spring spring-aop


    【解决方案1】:

    您是否已将 spring-aspects.jar 添加到项目的方面路径中?

    在项目属性中,在“AspectJ Build”->“Aspect Path”下尝试添加 spring-aspects.jar 并清理构建项目。

    抱歉,您可能已经这样做了 - 但您没有提及。

    【讨论】:

    • 我还没有真正添加它或进行任何特定配置(如上所述)。我似乎找不到任何关于 AJDT 插件的好的文档,所以我真的不知道我需要做什么。 :-/
    【解决方案2】:

    编译时编织似乎不起作用。尝试将以下行添加到您的 applicationcontext.xml

    <context:load-time-weaver />
    <context:spring-configured/>
    

    您可能还想将以下 xsd 添加到 xml 文件中

    http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

    详情请看这里:

    http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-aj-ltw

    【讨论】:

      【解决方案3】:

      您可以在没有 AspectJ 的情况下使用 @Transactional。您的配置文件应包含以下内容以使其正常工作:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-2.5.xsd"
        >
      <tx:annotation-driven/>
      

      告诉 spring 在创建配置的 bean 实例时寻找 @transactional 注释。在找到这样的注解时,spring 会将 bean 的动态代理返回给应用程序代码。这个动态代理确保无论何时调用带注释的方法,spring 都能够拦截它以提供预期的事务行为。但是基于代理的 AOP 要求您针对接口而不是具体类进行编码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多