AspectW方面不适用于LTW方案 [英] AspectJ aspect is not applied in LTW scenario

查看:127
本文介绍了AspectW方面不适用于LTW方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在独立应用程序中使用AspectJ,但似乎不起作用。

I am trying to use AspectJ in a standalone application but does not seem to work.

这是我创建的类 -

Here are the classes I created-

package oata.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AspectJTest {


    @Around("execution(* *..*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("around fired");
        jp.proceed();
    }
}



package oata;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface AspectTest {
}



package oata;

import oata.AspectTest;

public class TestAspect {
    public void doItWithout(int i) {
        double s = Math.acos(i);
    }

    @AspectTest
    public void doItAnnotated(int i) {
        double s = Math.acos(i);
    }

    public void doItAspect(int i) {
        double s = Math.acos(i);

    }
}



package oata;

import java.util.Date;

public class Test {
    public Test() {
    }

    public static void main(String arg[]) {
        // performance testing
        // invoke method without aspect
        long t1 = new Date().getTime();
        for (int i = 0; i < 1; i++) {
            new TestAspect().doItWithout(i);
        }
        System.out.println("Invoke without aspect:"
                + (new Date().getTime() - t1));
        // invoke method with annotated aspect
        t1 = new Date().getTime();
        for (int i = 0; i < 1; i++) {
            new TestAspect().doItAnnotated(i);
        }
        System.out.println("Invoke annotated aspect method:"
                + (new Date().getTime() - t1));
        // invoke method with aspect but not annotated
        t1 = new Date().getTime();
        for (int i = 0; i < 1; i++) {
            new TestAspect().doItAspect(i);
        }
        System.out.println("Invoke aspect method:"
                + (new Date().getTime() - t1));
    }
}

同样在src / META_INF文件夹下我创建了aop。 xml文件

Also under src/META_INF folder I have created aop.xml file

<aspectj>
    <aspects>
        <aspect name="oata.aspect.AspectJTest" />
    </aspects>
    <weaver>
        <include within="oata.*" />
    </weaver>
</aspectj>

然后从命令行尝试使用以下命令运行Test.java System.out建议中的.println没有打印 -

Then from the command line when I try running the Test.java using the below command the System.out.println in the advice does not get printed-

\TestAspectJ\bin>java -javaagent:D:\Project\workspaces\RCS_3.2.1\TestAspectJ\src\aspectjweaver-1.6.10.jar oata.Test

任何人都可以让我知道我做错了什么。

Can anyone please let me know what is it that I am doing wrong.

谢谢
AA

Thanks AA

推荐答案

几件事情:


  • 你的 META -INF / * 文件夹肯定被复制到您运行应用程序的bin文件夹中?

  • 您指定的包含 oata。* ,只包含oata包中的直接类,如果你想要更多的子包(我认为你这样做)你需要 oata .. *

  • 您是否尝试过指定 weaver options = - verbose - doe是什么给你看的?如果它没有显示任何内容,则找不到aop.xml文件。如果它确实显示了某些内容,它会告诉您哪些方面正在打开。也许然后用 -debug 来增加它以查看更多有关正在发生的事情。

  • 我可能包含 !在(AspectJTest)对你的切入点的投诉,或者当它开始工作时,你可能最终会自我建议并在堆栈溢出时失败。

  • 最后,我知道您现在没有使用它,但如果您打算使用该注释与AspectJ匹配,则需要从 SOURCE 保留更改它,因为AspectJ在字节代码级别工作并且不会看到它是否有源保留。

  • Is your META-INF/* folder definitely being copied to your bin folder where you are running the app from?
  • You are specifying an include of oata.*, that will only include direct classes in the oata package, if you want further sub packages (and I think you do) you need oata..*
  • Have you tried specifying weaver options="-verbose" - does that show you anything? If it shows you nothing the aop.xml file is not being found. If it does show you something it will tell you which aspects are being turned on. Perhaps then augment it with -debug to see more about what is going on.
  • I might include a !within(AspectJTest) complaint to your pointcut or you could end up self advising and failing with a stack overflow when it does start working.
  • Finally, I know you aren't using it now but if you intend to use that annotation for matching with AspectJ you will need to change it from SOURCE retention because AspectJ works at the byte code level and won't see if it has source retention.

这篇关于AspectW方面不适用于LTW方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆