Aspectj没有使用eclipse Luna

时间:2014-07-24 19:10:17

标签: eclipse eclipse-plugin aspectj

我正在尝试将aspectJ与eclipse 4.4.0(Luna)一起使用。 Y找不到适合这个版本的eclipse的任何ajdt插件。

无论如何我认为aspectj应该有效,所以我将aspectj jar放在我的webApp的lib目录中,并创建这个Aspect类:

@Aspect
public class LoggerAspect {

    @Before("(execution(* *.*(..))")
    public void intercept(JoinPoint jp) {
        System.out.print("before");
    }

    @After("(execution(* *.*(..))")
    public void afterReturning(JoinPoint jp) {
        System.out.print("after");
    }

    @AfterThrowing(pointcut = "execution(* *.*(..))")
    public void logException(JoinPoint jp, Exception t) {
        System.out.print("throwing");
    }
}

但没有记录任何内容。我错过了什么?

2 个答案:

答案 0 :(得分:19)

如果您在pom.xml中设置了AspectJ Maven Plugin,那么为了使其在Luna中运行,您似乎首先需要添加以下更新站点(菜单帮助,安装新软件):

http://download.eclipse.org/tools/ajdt/44/dev/update/

从该网站安装 AspectJ开发工具

安装完成后,您还可以通过Preferences,Maven,Discovery,Open Catalog安装 Maven Integration for AJDT ,然后选择 AspectJ m2e配置器

(尝试在安装工具之前执行最后一步,将抛出缺少要求:AspectJ开发工具核心2.1.3。尝试从默认设置安装AspectJ时抛出相同的错误Luna更新站点。)

答案 1 :(得分:4)

如果您想使用AJDT,请尝试使用Kepler。 ;-)

是的,您是否使用 ajc (AspectJ编译器)编译了您的方面和Java类?或者你想用 javac 编译它们?在这种情况下,您需要在运行时(LTW,加载时编织)中编织方面,并需要编织代理(-javaagent:path/to/aspectjweaver.jar)以及aop.xml中的相应配置。在你告诉我你想做什么之后,我可以用一个具体的例子来更新这个答案。


更新:好的,我测试了你的方面。它包含一些语法错误和其他问题,例如

  • @Before("(execution(* *.*(..))")中,数字括号不匹配。相反它应该是@Before("execution(* *.*(..))")
  • 投掷后注释错过了异常绑定。它应该是@AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "t")

现在这是一个完全可测试的独立示例:

驱动程序应用程序:

class Application {
    public static void main(String[] args) {
        new Application().foo();
    }

    public void foo() {
        try {
            sayHello("world");
        }
        catch (Exception e) {
            System.out.println("Caught exception: " + e); 
        }
    }

    public void sayHello(String recipient) {
        System.out.println("Hello " + recipient + "!"); 
        throw new RuntimeException("Oops!");
    }
}

<强>方面:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggerAspect {
    @Before("execution(* *.*(..))")
    public void intercept(JoinPoint jp) {
        System.out.println("before " + jp);
    }

    @After("execution(* *.*(..))")
    public void afterReturning(JoinPoint jp) {
        System.out.println("after " + jp);
    }

    @AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "t")
    public void logException(JoinPoint jp, Exception t) {
        System.out.println("throwing " + jp);
    }
}

如何编译&amp;使用 ajc

运行

假设您的源文件位于名为src的目录中,并且您希望类文件以bin结尾,这就是您编译的方式:

ajc -sourceroots src -1.7 -d bin -cp aspectjrt.jar

现在运行应用程序:

java -cp bin;aspectjrt.jar Application

before execution(void Application.main(String[]))
before execution(void Application.foo())
before execution(void Application.sayHello(String))
Hello world!
after execution(void Application.sayHello(String))
throwing execution(void Application.sayHello(String))
Caught exception: java.lang.RuntimeException: Oops!
after execution(void Application.foo())
after execution(void Application.main(String[]))

如何使用javac&amp;编译与LTW一起运行:

您需要使用调试符号(-g)进行编译:

javac -g -cp aspectjrt.jar -d bin src\*.java

在运行程序之前,您需要bin\META-INF\aop.xml(或aop-ajc.xml)下的LTW配置:

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <aspects>
        <aspect name="LoggerAspect"/>
    </aspects>
    <weaver>
        <include within="*"/>
    </weaver>
</aspectj>

现在您可以使用编织代理运行应用程序:

java -javaagent:aspectjweaver.jar -cp bin Application

输出相同,但如果你想看到更多,你可以添加其他编织选项,如

<weaver options="-verbose -showWeaveInfo">

这应该产生以下输出:

[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by before advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by after advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.main(java.lang.String[]))' in Type 'Application' (Application.java:3) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by before advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by after advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.foo())' in Type 'Application' (Application.java:8) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by before advice from 'LoggerAspect'(LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by after advice from 'LoggerAspect' (LoggerAspect.java)
[AppClassLoader@58644d46] weaveinfo Join point 'method-execution(void Application.sayHello(java.lang.String))' in Type 'Application' (Application.java:16) advised by afterThrowing advice from 'LoggerAspect' (LoggerAspect.java)
before execution(void Application.main(String[]))
before execution(void Application.foo())
before execution(void Application.sayHello(String))
Hello world!
after execution(void Application.sayHello(String))
throwing execution(void Application.sayHello(String))
Caught exception: java.lang.RuntimeException: Oops!
after execution(void Application.foo())
after execution(void Application.main(String[]))

如果您还需要更多,请将-debug选项添加到weaver选项中。