AspectJ:如何获取切入点来建议位于其他项目中的类
这应该很简单。
问题
如何在一个项目中获得切入点来为另一个项目中的代码/类提供建议?
Context
I'm working in eclipse with two projects. For ease of explanation, let's call one science project and the other math project and say the science project relies on the math project and I'm developing in both projects, concurrently. The math project is a core product, in production, and life will be easier if I don't modify the code much.
目前,我正在调试这两个项目之间的交互。为了帮助实现这一点,我正在编写一个方面(在科学项目内)来记录数学代码(和科学代码)执行时的关键信息。
Example
I running a simple example aspect along the lines of:
package org.science.example;
public aspect ScientificLog {
public pointcut testCut() : execution (public * *.*(..));
before() : testCut() {
//do stuff
}
}
Problem
The problem is, no matter what pointcut I create, it only advises code from the science project. No classes from
org.math.example
are crosscut, AT ALL!I tried adding the math project to the inpath of the science project by going to
proect properties > AspectJ Build > Inpath
and clicking add project and choosing the math project. That didn't work but it seems like I need to do something along those lines.预先感谢您的任何建议...-
gMale
EDIT 1:
Since writing this, I've noticed the project is giving the following error:
Caused by: org.aspectj.weaver.BCException: Unable to continue, this version of AspectJ supports classes built with weaver version 6.0 but the class com.our.project.adapter.GenericMessagingAdapter is version 7.0 when batch building BuildConfig[null] #Files=52 AopXmls=#0
所以也许这是设置正确并且错误更微妙。顺便说一句,可以说,提到的课程来自“科学项目”。即使在我清理项目后也会发生这种情况。我目前正在谷歌搜索这个错误......
EDIT 2:
I found the solution to the error above in comment #5 here
问题是maven-aspectj-plugin的pom文件声明了对aspectjtools版本1.6.7的依赖。因此,在配置插件时,必须修改临时依赖项。以下是 pom 文件的相关代码片段,通过指定版本 1.6.9 而不是 1.6.7 来修复问题:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.9</version>
</dependency>
</dependencies>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
This should be simple.
Question
How do you get a pointcut in one project to advise the code/classes within another project?
Context
I'm working in eclipse with two projects. For ease of explanation, let's call one science project and the other math project and say the science project relies on the math project and I'm developing in both projects, concurrently. The math project is a core product, in production, and life will be easier if I don't modify the code much.
Currently, I'm debugging the interaction between these two projects. To assist with that, I'm writing an Aspect (within the science project) to log key information as the math code (and science code) executes.
Example
I running a simple example aspect along the lines of:
package org.science.example;
public aspect ScientificLog {
public pointcut testCut() : execution (public * *.*(..));
before() : testCut() {
//do stuff
}
}
Problem
The problem is, no matter what pointcut I create, it only advises code from the science project. No classes from
org.math.example
are crosscut, AT ALL!I tried adding the math project to the inpath of the science project by going to proect properties > AspectJ Build > Inpath
and clicking add project and choosing the math project. That didn't work but it seems like I need to do something along those lines.
Thanks, in advance, for any suggestions...
-gMale
EDIT 1:
Since writing this, I've noticed the project is giving the following error:
Caused by: org.aspectj.weaver.BCException: Unable to continue, this version of AspectJ supports classes built with weaver version 6.0 but the class com.our.project.adapter.GenericMessagingAdapter is version 7.0 when batch building BuildConfig[null] #Files=52 AopXmls=#0
So maybe this is setup properly and the error is more subtle. BTW, the class mentioned is from the "science project," so to speak. This happens even after I clean the project. I'm currently googling this error...
EDIT 2:
I found the solution to the error above in
comment #5 here
The problem is the maven-aspectj-plugin's pom file declares a dependency on aspectjtools version 1.6.7. So, when configuring the plugin, that transient dependency has to be modified. Here's the related code snippet for the pom file that fixes the problem by specifying version 1.6.9 instead of 1.6.7:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.9</version>
</dependency>
</dependencies>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将数学项目添加到科学项目的路径中时,所有数学项目的代码都会通过aspectj weaver发送并正确编织。该编织的结果将写入科学项目的输出文件夹(而不是数学项目的)。因此,如果您要查看科学项目的 bin 文件夹,您应该会看到那里的编织类。
如果您希望将 inpath 文件与常规文件分开,您可以指定一个 inpath out 文件夹。该文件夹还应作为二进制文件夹添加到类路径中。此外,该文件夹应放置在 Science 项目的 Java 构建页面的“导出和排序”选项卡中对 Math 项目的项目依赖项之上。
最后,如果您从科学项目而不是数学项目运行主类,您将执行编织代码。
When you add Math project to the in path of science project, all of math project's code is sent through the aspectj weaver and properly woven. The results of that weave are written to science project's output folder (not Math project's). So, if you were to look in science project's bin folder, you should see the woven classes there.
If you wanted to keep the in path files separate from the regular files, you can specify an inpath out folder. This folder should also be added to the class path as a binary folder. Also, this folder should be placed above the project dependency to Math project in the "Export and Order" tab of the Java build page for Science project.
Finally, if you run the main class from Science project, rather than from Math project, you will be executing the woven code.
你的第二个问题与第一个问题无关。据说 com.our.project.adapter.GenericMessagingAdapter 最初是针对新版本的 AspectJ 进行编译和编织的,但现在用于针对旧版本的 AspectJ 进行二进制编织。
这本质上与您尝试在 1.5 VM 上运行在 1.6 下编译的 Java 类时遇到的问题相同。
AspectJ 1.6.8 的版本号已加快(我想,或者可能是 1.6.7)。
解决方案是确保您的所有项目都使用最新版本的 AspectJ(例如 1.6.9 或 1.6.10 的开发版本)。
Your second problem is unrelated to the first. It is saying that com.our.project.adapter.GenericMessagingAdapter was originally compiled and woven against a new version of AspectJ but is being used to binary weave against an older version of AspectJ.
This is essentially the same problem as when you try to run Java classes compiled under 1.6 on a 1.5 VM.
The version number was revved up for the release of AspectJ 1.6.8 (I think, or maybe it was 1.6.7).
The solution is to make sure you are using the latest version of AspectJ for all of your projects (eg- 1.6.9, or dev builds of 1.6.10).