0

我正在开发一个具有多个插件的 rcp 项目,并且我正在使用 AJDT aspectJ 在应用程序中进行日志记录。我创建了两个方面,一个用于信息记录,一个用于每个插件中的异常记录。Aspect 对于基本插件工作正常,但不适用于其他插件。

我对上述实现几乎没有疑问:

  1. 这是为每个插件定义一个方面的正确方法还是我们可以为所有插件创建一个方面
  2. 如果不同插件中的包名称相同,那么在为每个插件创建单独的方面时是否会出现问题。
  3. 我试图为正常日志记录和异常日志记录创建一个方面,但对我不起作用。附加正常方面和异常方面的示例。
  4. 有没有办法在不提供包名称的情况下定义一个方面,我认为当我在切入点中提到包名称时,它会产生问题。

如果每个插件都有一个方面,我会遇到以下错误:

Caused by: org.aspectj.lang.NoAspectBoundException: com_tsystems_rvs_client_gui_user_RvsUserAspect
at com.tsystems.rvs.client.gui.user.RvsUserAspect.aspectOf(RvsUserAspect.aj:1)
at com.tsystems.rvs.client.gui.user.RvsUserAspect.<clinit>(RvsUserAspect.aj:27)

用于记录 info msg 的正常方面的代码

public aspect RvsFrameworkAspect {


public pointcut scope(): within(com.tsystems.rvs.client.gui.framework.*);

before() : scope(){
    Signature sig=thisJoinPointStaticPart.getSignature();
    String infoFormat="Entering ["+sig.getDeclaringType().getName()+"."+sig.getName();
    logTrace(infoFormat, thisJoinPointStaticPart, thisEnclosingJoinPointStaticPart);
}

after() : scope(){
    Signature sig=thisJoinPointStaticPart.getSignature();
    String infoFormat="Leaving ["+sig.getDeclaringType().getName()+"."+sig.getName()+"]";
    logTrace(infoFormat, thisJoinPointStaticPart, thisEnclosingJoinPointStaticPart);
}
protected synchronized void logTrace(String info, StaticPart location,
        StaticPart enclosing) {

        Signature signature = location.getSignature();

        String source = signature.getDeclaringTypeName() + ":" + 
            (enclosing.getSourceLocation().getLine());
       LoggerMode.getInstance().logInfo(" " + source + " - " + info);


}

}

插件中的日志异常代码

public aspect RvsFrameworkExceptionAspect {

public pointcut scope(): within(com.tsystems.rvs..*);
private Map<Throwable, String> loggedThrowables = new WeakHashMap<Throwable, String>();

after() throwing(Throwable t): scope() {

    logThrowable(t, thisJoinPointStaticPart, 
            thisEnclosingJoinPointStaticPart);
    }

before (Throwable t): handler(Exception+) && args(t) && scope() {

    logThrowable(t, thisJoinPointStaticPart, 
            thisEnclosingJoinPointStaticPart);
    }
protected synchronized void logThrowable(Throwable t, StaticPart location,
        StaticPart enclosing) {

    if (!loggedThrowables.containsKey(t)) {
        loggedThrowables.put(t, null);

        Signature signature = location.getSignature();

        String source = signature.getDeclaringTypeName() + ":"
                + (enclosing.getSourceLocation().getLine());


            LoggerMode.getInstance().logError(" " + source + " - " + t.toString(), t);

    }
}

}

请帮助我在我的实施中做错了什么。

4

1 回答 1

1

可能您的切入点RvsFrameworkExceptionAspect只是建议您要排除的类。具体来说,

within(com.tsystems.rvs..*)

将匹配

com.tsystems.rvs.client.gui.user.RvsUserAspect,

即,您似乎在建议另一个方面。尝试更具体地了解您要拦截的内容。


在评论中编辑您的其他问题:

如果你的切面都有以“Aspect”结尾的类名,你可以试试这个:

within(com.tsystems.rvs..*) && !within(*..*Aspect)

否则,您可以排除单个方面类或只包含更少的非方面类,无论您的情况更简单。通常,在不了解您的完整代码库的情况下很难提供有用的提示,因此请理解我可能不会像您希望的那样准确地达到目标。;-)

于 2012-08-30T12:05:02.270 回答