Eclipse:如何将Web项目转换为AspectJ项目并使用AJDT插件编织并运行它?

发布于 2024-07-23 08:58:53 字数 1913 浏览 7 评论 0原文

我想要做什么:

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

我所做的:

  • 安装了 Eclipse 3.4 的 AJDT:AspectJ 开发工具 插件。
  • 右键单击我的 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

What I want to do:

  • I want to use the @Configured annotation with Spring. It requires AspectJ to be enabled. I thought that using the AJDT plugin for compile time weaving would solve this problem. Before installing the plug in the dependencies which were supposed to be injected into my @Configured object remained null.

What I have done:

  • Installed the AJDT: AspectJ Development Tools plug in for Eclipse 3.4.
  • Right clicked on my web project and converted it into a AspectJ project.
  • Enabled compile time weaving.

What doesn't work:

  • When I start the Tomcat 6 server now, I get an exception*.

Other information:

  • I haven't configured anything in the AspectJ Build and AspectJ Compiler parts of the project properties.
  • JDT Weaving under Preferences says weaving is enabled.
  • I still have Java build path and Java Compiler under project properties. And they look like I previously configured them (while the above two new entries are not configured).
  • The icon of my @Configured object file looks like any other file (i.e. no indication of any aspect or such, which I think there should be). The file name is MailNotification.java (and not .aj), but I guess it should still work as I'm using a Spring annotation for AspectJ?
  • I haven't found any tutorial or similar which teaches: How to turn a Spring web application project into an AspectJ project and weave aspects into the files using the AJDT plugin, all within Eclipse 3.4. If there is anything like that out there I would be very interested in knowing about it.

What I would like to know:

  • Where to go from here? I just want to use the @Configured annotation of Spring. I'm also using @Transactional which I think also needs AspectJ.
  • If it is possible I would like to study AspectJ as little as possible as long as my needs are met. The subject seems interesting, but huge, all I want to do is use the above two mentioned Spring annotations.

*** Exception when Tomcat 6 is started:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的影子我的梦 2024-07-30 08:58:53

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

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

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

Have you added spring-aspects.jar to your aspect path for the project?

In the project properties, under 'AspectJ Build' -> 'Aspect Path' try adding spring-aspects.jar and clean building the project.

Sorry you might have already done this - but you didn't mention it.

橘虞初梦 2024-07-30 08:58:53

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

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

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

http://www.springframework.org/schema/context http://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

It looks like the compile time weaving isn't working. Try adding the below lines to your applicationcontext.xml

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

You'll probably want to add the following xsd to the xml file also

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

For details see here:

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

一笔一画续写前缘 2024-07-30 08:58:53

您可以在没有 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 要求您针对接口而不是具体类进行编码。

You can use @Transactional without AspectJ. Your configuration file should contain something like following to make it work:

<?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/>

tells spring to look for @transactional annotations when creating instances of configured beans. On finding such annotation, spring returns a dynamic proxy of the bean to the application code. This dynamic proxy ensures that whenever the annotated methods are called, spring is able to intercept it to provide intended transactional behavior. But the proxy-based AOP mandates that you code against interfaces and not concrete classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文