如何使用AspectJ捕获玉器的主要方法痕迹? [英] how capture the main method trace from jade using AspectJ?
问题描述
我正在eclipse IDE中使用Jade,我想从jade中捕获主要方法,因为主要方法是每个应用程序的起点,因此我想测试一下JADE中间件的运行是否正确(即:我想检查我启动玉器中间件时是否执行了main方法)
i已经这样做了:
i am using Jade in eclipse IDE, i want to capture the main method from jade, because the main method is the starting point for every application, i want to test if it is also right with the running of the JADE middleware (ie: i want to check if the main method is executed when i start the jade middleware or not) i already did this :
public aspect MainAspect {
pointcut main() : execution(public static void main(..));
before() : main(){
System.out.println("main is executed");
}
}
,但没有捕获任何内容;有什么意见吗?
谢谢
but it is not capturing anything; is there any comment ? thanks
推荐答案
好吧,我有一段时间找出你自己还没有告诉我的内容。我的假设是正确的, JADE 是第三方库。
Okay, I had some time to find out what you have not told me by myself. My assumption was correct that JADE is a third-party library.
这意味着您要将方面代码编织到该库中包含的 main
方法中。通常,在编译时进行编织时,在编译过程中只会编织您自己项目中的类,这说明了为什么未拦截外部 main
方法的原因。
This means that you want to weave aspect code into a main
method contained within that very library. Normally with compile-time weaving only classes from your own project are woven during compilation, which explains why the external main
method is not intercepted.
那么如何将方面代码编织到JADE的 main
方法中?有两种方法:
So how do you go about getting your aspect code woven into JADE's main
method? There are two ways:
-
编译时二进制编织:将库放在编译器的 inpath ,导致它吐出库中找到的所有Java类的编织版本。可以将它们重新打包到检测的库JAR中,然后在您的应用程序中使用。但这也不是很灵活,也很麻烦。
Compile-time binary weaving: You put the library on the compiler's inpath, causing it to spit out woven versions of all Java classes it finds in the library. Those can be re-packaged into an instrumented library JAR and then used from within your application. But this is not very flexible and quite intrusive, too.
加载时编织(LTW):您放置了 -javaagent:/path/to/aspectjweaver.jar
在JVM命令行上,还指定一个 aop.xml (或 aop-ajc.xml )文件,告诉织布工将哪些方面编织到哪些包或类中,从而导致AspectJ在类加载期间动态应用方面。这是灵活且微创的。
Load-time weaving (LTW): You put -javaagent:/path/to/aspectjweaver.jar
on the JVM command line and also specify an aop.xml (or aop-ajc.xml) file telling the weaver which aspects to weave into which packages or classes, causing AspectJ to dynamically apply aspects during classloading. This is flexible and minimally invasive.
示例:
假设您使用Eclipse来实现所需的最简单方法就是创建两个项目,
Assuming you use Eclipse the easiest way to achieve what you want is to create two projects,
- AspectJ包含您的方面的项目和
- 包含您的JADE代理的纯Java项目。
它看起来像这样:
虚拟JADE代理:
package de.scrum_master.app;
import jade.core.Agent;
public class BookBuyerAgent extends Agent {
protected void setup() {
System.out.println("Hello! Buyer-agent " + getAID().getName() + " is ready.");
}
}
方面:
package de.scrum_master.aspect;
public aspect MainAspect {
before() : execution(public static void main(..)) {
System.out.println(thisJoinPoint);
}
}
LTW配置 META_INF / aop -ajc.xml :
LTW configuration META_INF/aop-ajc.xml:
创建LTW运行配置后,该文件将自动生成(见下文)。
This file will be auto-generated as soon as you create an LTW run configuration (see below).
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="de.scrum_master.aspect.MainAspect"/>
</aspects>
</aspectj>
AspectJ LTW运行配置:
现在选择Java项目(而不是AspectJ项目为 not )并为其创建运行配置:从Eclipse菜单中选择运行-运行配置,然后选择类别然后单击 AspectJ加载时编织应用程序,然后单击新启动配置按钮。然后指定以下信息:
Now select your Java project (not the AspectJ project) and create a run configuration for it: Select "Run" - "Run Configurations" from the Eclipse menu, then select category "AspectJ Load-Time Weaving Application" and click the "New launch configuration" button. Then specify the following information:
1。)项目名称(应预先选择)和主类
1.) Project name (should be preselected) and main class
< img src = https://i.stack.imgur.com/blowL.png alt = LTW运行配置-主类>
2。)程序参数
3。)使用添加项目按钮将AspectJ项目添加到所谓的 aspectpath
3.) Use "Add projects" button to add the AspectJ project to the so-called aspectpath
控制台输出应类似于以下内容:
The console output should look similar to this:
execution(void jade.Boot.main(String[]))
Mrz 15, 2015 5:36:18 PM jade.core.Runtime beginContainer
Information: ----------------------------------
This is JADE 4.3.3 - revision 6726 of 2014/12/09 09:33:02
downloaded in Open Source, under LGPL restrictions,
at http://jade.tilab.com/
----------------------------------------
(...)
Hello! Buyer-agent buyer@192.168.178.33:1099/JADE is ready.
Mrz 15, 2015 5:36:19 PM jade.core.AgentContainerImpl joinPlatform
(...)
第一行显示 main
方法实际上是由AspectJ拦截的。话虽如此,我真的很想知道为什么要检查是否已调用它,因为如果不是JADE容器,则无论如何都不会启动它。 ;-)
The very first line shows that the main
method was actually intercepted by AspectJ. Having said that, I really wonder why you want to check if it was invoked because if it was not the JADE container would not have been started anyway. ;-)
这篇关于如何使用AspectJ捕获玉器的主要方法痕迹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!