我想在NetBeans中使用编译时AspectJ和Ant 。我想在Google App Engine上运行它,但目前并不重要。 AspectJ是基于注释的。
我更喜欢编译时编织(类的修改,检测)。我不想使用自定义类加载器。怎么做到这一点?
我尝试使用NetBeans AspectJ Annotation Tutorial。我修改了build.xml来处理aspectj(使用iajc Ant task),如here所述。问题是它需要添加-javaagent:lib/aspectjweaver.jar
(在GAE上不可能)。
运行我的构建生成此输出:
info compiling C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java
weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by before advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:17(from BasicAspect.java))
weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by after advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:23(from BasicAspect.java))
info woven class net.andrewewhite.examples.HelloWorld (from C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java)
info Compiler took 2547ms
当我使用-javaagent
参数运行我的项目时,它可以正常工作。 (在NetBeans中:单击项目>属性>运行> VM选项:-javaagent:./ dist / lib / aspectjweaver.jar)。来自tutorial的代码输出:
run:
About to make call to print Hello World
Hello World!
Just made call to print Hello World
BUILD SUCCESSFUL (total time: 0 seconds)
没有代理(清除了VM选项)代码就像没有AspectJ一样运行:
run:
Hello World!
BUILD SUCCESSFUL (total time: 0 seconds)
\ TryAspectJ \ SRC \ META-INF \ aop.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<aspect name="net.andrewewhite.aspects.BasicAspect" />
</aspects>
</aspectj>
\ TryAspectJ \ src \ net \ andrewewhite \ aspects \ BasicAspect.java
package net.andrewewhite.aspects;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class BasicAspect {
@Before(" call(void java.io.PrintStream.println(java.lang.String)) " +
"&& !within(net.andrewewhite.aspects..*)")
public void beforePrintlnCall() {
System.out.println("About to make call to print Hello World");
}
@After(" call(void java.io.PrintStream.println(java.lang.String)) " +
"&& !within(net.andrewewhite.aspects..*)")
public void afterPrintlnCall() {
System.out.println("Just made call to print Hello World");
}
}
\ TryAspectJ \ SRC \净\ andrewewhite \例子\ HelloWorld.java
package net.andrewewhite.examples;
public class HelloWorld {
public static void main(String[] argv) {
System.out.println("Hello World!");
}
}
\ TryAspectJ \的build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="TryAspectJ" default="default" basedir=".">
<description>Builds, tests, and runs the project TryAspectJ.</description>
<import file="nbproject/build-impl.xml"/>
<taskdef classpath="lib/aspectj/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/>
<target name="aspectj">
<echo level="info">--- aspectj (start) ---</echo>
<iajc destDir="${build.classes.dir}" source="1.6" target="1.6" showweaveinfo="true" verbose="true" >
<inpath>
<pathelement location="lib/aspectj/aspectjrt.jar"/>
<pathelement location="${build.classes.dir}" />
</inpath>
<sourceroots>
<pathelement location="${src.dir}"/>
</sourceroots>
<classpath>
<pathelement location="${javac.classpath}"/>
<pathelement location="${j2ee.platform.classpath}"/>
</classpath>
</iajc>
<echo level="info">--- aspectj finished ---</echo>
</target>
<target name="-post-compile" depends="aspectj"></target>
</project>
我需要添加或更改哪些内容?
答案 0 :(得分:4)
找到解决方案。 这是正确的build.xml片段:
<taskdef classpath="lib/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/>
<target name="aspectj">
<echo level="info">--- aspectj (start) ---</echo>
<!-- begin fix classpath for this bug https://issues.apache.org/bugzilla/show_bug.cgi?id=40291 -->
<condition property="targetos" value="windows" else="unix">
<os family="windows"/>
</condition>
<!-- converting classpath -->
<pathconvert targetos="${targetos}" property="javac.convertedClasspath" >
<path path="${javac.classpath}" />
</pathconvert>
<!-- end fix classpath -->
<iajc source="1.6" target="1.6" showweaveinfo="true" verbose="true" destdir="${build.classes.dir}" >
<inpath>
<pathelement location="${build.classes.dir}"/>
</inpath>
<classpath>
<pathelement location="${javac.convertedClasspath}" />
</classpath>
</iajc>
<echo level="info">--- aspectj (finished) ---</echo>
</target>
<target name="-post-compile" depends="aspectj"></target>
调用java -classpath ./lib/aspectjrt.jar -jar TryAspectJ.jar
可以正常工作。奇怪的是,从NetBeans(项目 - >运行中的右侧clik)结果就好像没有AspectJ一样。我认为NetBeans从build/classes
目录运行项目而不是jar。但这不是问题。
答案 1 :(得分:0)
您需要将方面路径元素添加为iajc元素的子元素。
<aspectpath>
<pathelement location="..." />
</aspectpath>
只有AspectJ加载时编织才需要aop.xml
文件。