本文介绍了AspectJ-为什么“未应用XYZ中定义的建议"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用AspectJ(1.6.11).我正在通过commons-email libary发送电子邮件,我想知道发送一条消息需要多长时间.这是我的电子邮件发送代码:

I just started playing with AspectJ (1.6.11). I'm sending emails via commons-email libary and I'd like to know how long it takes to send a message. So this is my email sending code:

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class EmailTest
{
    public static void main(String[] args) throws EmailException
    {
        Email e = new SimpleEmail();
        e.setHostName("localhost");
        e.setFrom("foo@localhost");
        e.addTo("batto@localhost");
        e.setSubject("Test " + System.currentTimeMillis());
        e.setMsg("Message");
        e.send();
    }
}

这是我的方面:

public aspect EmailAspect
{
    private long start;

    pointcut conn() : call(* javax.mail.Transport.send(..));

    void around() : conn() {
        start = System.currentTimeMillis();
        Throwable t = null;
        try {
            proceed();
        }
        catch(Throwable _t) {
            t = _t;
        }
        long spent = System.currentTimeMillis() - start;
        System.out.println("Send time: " + spent);
        if (t != null) {
            throw new RuntimeException(t);
        }
    }
}

但是,当我使用以下代码进行编译时:

However when I compile it with:

java -jar ../lib/aspectjtools-1.6.11.jar -cp "$CLASSPATH" -source 6 Email*{java,aj}

我收到以下警告:

/home/batto/work/ajtest/test/EmailAspect.aj:8 [warning] advice defined in EmailAspect has not been applied [Xlint:adviceDidNotMatch]


1 warning

当然,方面不起作用.我尝试过before()/after()建议,call()中的不同模式,但都以相同的警告告终.

And of course aspect doesn't work. I tried before()/after() advices, different patterns in call(), but it all ended up with same warning.

我在Eclipse中调试了程序(带有commons-email源),并且我知道Transport.send()被执行了.

I debugged the the program in Eclipse (with commons-email sources) and I know that Transport.send() gets executed.

出什么问题了?

谢谢.

更新

因此,我刚刚确定需要使用javax.mail.Transport的源代码来进行编织(源编织).因此,我使用了二元编织技术,而且效果很好.但是我改变了主意,现在我想测量java.net.Socket.connect(..)的时间(org.apache.commons.mail.Email.send(..)调用javax.mail.Transport.send(. .)调用java.net.Socket.connect(..),我通过在Eclipse中进行调试对其进行了验证.但是我不想在我的编译代码中包含整个JDK.因此,我尝试了加载时编织.这是我修改过的方面:

So I've just figured out that I need source code of javax.mail.Transport for the method of weaving I used (source weaving). So I used binary weaving and it worked. However I changed my mind and now I want to measure time of java.net.Socket.connect(..) (org.apache.commons.mail.Email.send(..) calls javax.mail.Transport.send(..) which calls java.net.Socket.connect(..), I verified it by debugging in Eclipse). But I don't want to include whole JDK in my compiled code. So I tried load-time weaving. This is my modified aspect:

public aspect EmailAspect {
    // Some day I'll learn what's the difference between call() and execution()
    pointcut conn() :
        call(* java.net.Socket.connect(..)) ||
        execution(* java.net.Socket.connect(..));

    before() : conn() {
        System.out.println("It works");
    }
}

这是我编译&运行程序:

And this steps I did to compile & run program:

$ mkdir META-INF
$ cat >META-INF/aop.xml
<aspectj>
  <aspects>
    <aspect name="EmailAspect"/>
    <!-- I think this is not neccessary -->
    <include within="java..*"/>
    <include within="javax..*"/>
  </aspects>
  <weaver>
    <include within="java..*"/>
    <include within="javax..*"/>
  </weaver>
</aspectj>
$ # add jars from ../lib to CLASSPATH
$ javac -source 6 EmailTest.java
$ java -jar ../lib/aspectjtools-1.6.11.jar -cp ".:$CLASSPATH" -source 6 EmailAspect.aj
$ java -cp ".:$CLASSPATH" -javaagent:../lib/aspectjweaver-1.6.11.jar EmailTest

但是它不起作用:(.

推荐答案

因此,我已经解决了加载时编织的问题.我将aop.xml更改为:

So I've just sovled the problem with load-time weaving. I changed aop.xml to:

<aspectj>
  <aspects>
    <aspect name="EmailAspect"/>
  </aspects>
  <weaver options="-verbose -Xset:weaveJavaxPackages=true -Xset:weaveJavaPackages=true">
  </weaver>
</aspectj>

我在这里找到了答案 https://bugs.eclipse.org/bugs/show_bug.cgi?id = 149261#c11 (还有AspectJ运行时报告我需要一直提供-Xset:weaveJavaxPackages = true选项,我实在太笨了:().

I found answer here https://bugs.eclipse.org/bugs/show_bug.cgi?id=149261#c11 (and also AspectJ runtime was reporting that I need to supply -Xset:weaveJavaxPackages=true option all the time, I was just too dumb :().

这篇关于AspectJ-为什么“未应用XYZ中定义的建议"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 14:48