Eclipse:如何将Web项目转换为AspectJ项目并使用AJDT插件编织并运行它?
我想要做什么:
- 我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否已将 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.
看起来编译时编织不起作用。 尝试将以下行添加到您的 applicationcontext.xml
您可能还想将以下 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
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
您可以在没有 AspectJ 的情况下使用 @Transactional。 您的配置文件应该包含类似以下内容才能使其工作:
告诉 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:
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.