我是aspectj的新手。我需要用 aspectj 实现登录 ant 项目。我在网上看过很多教程,我尝试做一些非常简单的事情,但它不起作用。Mabye我什么都没注意到?
我有 2 个 ant 模块——HelloWorld 和 AspectModule,它们非常简单。HelloWorld.java:
package oata;
public class HelloWorld {
public void doSmth() {
System.out.println("do smth");
}
public static void main(String[] args) {
System.out.println("Hello World!!!");
HelloWorld hw = new HelloWorld();
hw.doSmth();
hw.doSmth();
}
}
以及 AspectModule 中的 LogAspect.aj:
import java.util.logging.Logger;
public aspect LogAspect {
private static Logger LOGGER = Logger.getLogger(LogAspect.class.getName());
pointcut logMethod() :
execution(* oata.HelloWorld.*(..));
before() : logMethod() {
LOGGER.info("HELLO FROM ASPECTJ!!!");
System.out.println("HELLO FROM ASPECTJ!!!");
}
}
两个模块都在编译和构建,但是当我运行 HelloWorld 时,LogAspect 没有输出。我做错了什么??
非常感谢您提前回答。
UPD:方面模块 (AspectsModule) 的 build.xml。
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="compile" name="aspects">
<property name="src" value="src"/>
<property name="build" value=".build"/>
<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">
<classpath>
<pathelement location="lib/aspectjtools-1.7.3.jar"/>
</classpath>
</taskdef>
<target name="clean">
<delete dir="${build}"/>
</target>
<target name="compile">
<mkdir dir="${build}"/>
<iajc destdir="${build}" sourceroots="${src}">
<classpath>
<pathelement location="lib/aspectjrt-1.7.3.jar"/>
</classpath>
</iajc>
</target>
</project>
HelloWorld 模块的 build.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Train" basedir="." default="build">
<property name="build.dir" value="${basedir}/.build"/>
<property name="build.classes.dir" value="${basedir}/.build/classes"/>
<property name="main-class" value="oata.HelloWorld"/>
<property name="lib.dir" value="lib"/>
<property name="modules.dir" value=".."/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="build.aspects">
<ant dir="${modules.dir}/AspectsModule" inheritall="true" />
</target>
<target name="init">
<antcall target="build.aspects"/>
</target>
<target name="compile" depends="init">
<mkdir dir="${build.classes.dir}"/>
<javac srcdir="src" destdir="${build.classes.dir}" classpathref="classpath"/>
</target>
<target name="build" depends="compile">
<mkdir dir="${build.dir}/jar"/>
<jar destfile="${build.dir}/jar/${ant.project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="oata.HelloWorld"/>
</manifest>
</jar>
</target>
<target name="run" depends="build">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${build.dir}/jar/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>