【发布时间】:2012-08-30 10:38:34
【问题描述】:
我正在开发一个具有多个插件的 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);
}
}
}
请帮助我在我的实施中做错了什么。
【问题讨论】: