【问题标题】:AspectJ Can't run @Before adviceAspectJ 无法运行 @Before 建议
【发布时间】:2014-02-09 14:52:31
【问题描述】:

开发者们好日子。

我刚开始学习 AspectJ AOP 风格。 请帮我。我正在尝试了解 LTW(加载时间编织),因为我对使用 AspectJ 进行嵌套方法调用更感兴趣,并且代码如下:

包 pkg.aop.target;

public class AppOut {


    public void methodOutside() {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Invoking AppOut.methodOutside()");  

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }   
        methodInside();

       }


   public void methodInside() {



       System.out.println("Invoking AppOut.methodInside()");

        }



    public static void main(String[] args) {

        new AppOut().methodOutside();

    }

}

以及方面部分:

package pkg.aop.myaspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class MyAspect {

    @Pointcut("call(* *.*(..))")
    void anyCallToMethod() {
    }


    @Before("anyCallToMethod()")
    public void beforeLogicAspect() {

        System.out.println("Should invoke before every method from AppOut class.");

    }

}

在 Eclipse 的 JVM 参数中,我有:

-javaagent:C:\Users\Vadim\workspace\aspectjweaver-1.7.4.jar

当我运行时,我看不到任何运行前的方法调用建议,只看我的方法消息。我做错了什么??

我的输出是:

Invoking AppOut.methodOutside()
Invoking AppOut.methodInside()

谢谢。

【问题讨论】:

    标签: java aop aspectj


    【解决方案1】:

    将切入点更改为

    call(* *(..))
    

    【讨论】:

    • 现在我有 @Pointcut("call(* *(..))") 但它并没有改变情况。我之前仍然看不到建议方法。如果我的 Aspect 和我的 AppOut 类在不同的包中可以吗?
    • 对。我没有注意那个。对不起。那么切入点似乎很好。你如何编译这个?马文?
    • 我没有编译它。我刚刚从 Eclipse 运行并期望看到它工作?我必须编译它吗?
    • 我不确定 Eclipse 做了什么。很明显,方面代码并没有融入您的代码中。你究竟是如何在 Eclipse 中运行它的?
    • 只是常规方式。单击菜单顶部的“内有白色三角形的绿色按钮”。就像我会在 Eclipse 中运行任何简单的应用程序一样
    【解决方案2】:
    pointcut anyCallToMethod(): execution(public void *(..) ) ;
    

    【讨论】:

    • 先生,我要把它放在哪里?我没有方面类我有常见的 java 类。它将如何工作
    • 哦,对不起,使用执行而不是调用不会带来正确的行为?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多