我正在开发一个有多个插件的rcp项目,我正在使用AJDT aspectJ在应用程序中进行日志记录。我创建了两个方面,一个用于信息日志记录,另一个用于每个插件中的异常日志记录.Aspect适用于基本插件,但它不适用于其他插件。
我对上述实施方面的疑问很少:
如果在每个插件中有一个方面,我会收到以下错误:
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)
用于记录信息消息的正常方面的代码
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);
}
}
}
请帮助我在实施过程中出错。
答案 0 :(得分:1)
RvsFrameworkExceptionAspect
中的切入点可能只是建议您要排除的课程。具体地,
within(com.tsystems.rvs..*)
将匹配
com.tsystems.rvs.client.gui.user.RvsUserAspect
,
即。你似乎正在建议另一个方面。尝试更加具体地说明你想拦截的内容。
修改:
如果您的方面都具有以“Aspect”结尾的类名,则可以尝试此操作:
within(com.tsystems.rvs..*) && !within(*..*Aspect)
否则,您可以排除单个方面类或只包含更少的非方面类,无论您的情况更简单。一般来说,在不知道完整代码库的情况下很难提供有用的提示,所以请理解我可能没有按照您的意愿完全达到目标。 ; - )