各种切入点表达式范围意外触发多个通知调用 [英] Various pointcut expression scopes trigger multiple advice calls unexpectedly
问题描述
使用方面记录项目,以便所有用 @Log
注释标记的方法、类和构造函数都将信息写入日志文件.
Logging a project using aspects such that all methods, classes, and constructors that are marked with the @Log
annotation have information written to a log file.
方法似乎是递归调用一级深度,但代码没有显示任何这样的递归关系.
Methods appear to be called recursively one-level deep, but the code does not show any such recursive relationship.
记录结果:
2018-09-25 12:17:29,155 |↷| EmailNotificationServiceBean#createPayload([SECURE])
2018-09-25 12:17:29,155 |↷| EmailNotificationServiceBean#createPayload([{service.notification.smtp.authentication.password=password, mail.smtp.port=25, service.notification.smtp.authentication.username=dev@localhost, mail.mime.allowutf8=true, mail.smtp.auth=false, mail.smtp.starttls.enable=false, mail.smtp.timeout=10000, mail.smtp.host=localhost}])
2018-09-25 12:17:29,193 |↷| EmailPayloadImpl#<init>([{service.notification.smtp.authentication.password=password, mail.smtp.port=25, service.notification.smtp.authentication.username=dev@localhost, mail.mime.allowutf8=true, mail.smtp.auth=false, mail.smtp.starttls.enable=false, mail.smtp.timeout=10000, mail.smtp.host=localhost}])
2018-09-25 12:17:29,193 |↷| EmailPayloadImpl#validate([SECURE])
2018-09-25 12:17:29,194 |↷| EmailPayloadImpl#validate([{service.notification.smtp.authentication.password=password, mail.smtp.port=25, service.notification.smtp.authentication.username=dev@localhost, mail.mime.allowutf8=true, mail.smtp.auth=false, mail.smtp.starttls.enable=false, mail.smtp.timeout=10000, mail.smtp.host=localhost}, SMTP connection and credentials])
2018-09-25 12:17:29,195 |↷| EmailPayloadImpl#setMailServerSettings([SECURE])
2018-09-25 12:17:29,196 |↷| EmailPayloadImpl#setMailServerSettings([{service.notification.smtp.authentication.password=password, mail.smtp.port=25, service.notification.smtp.authentication.username=dev@localhost, mail.mime.allowutf8=true, mail.smtp.auth=false, mail.smtp.starttls.enable=false, mail.smtp.timeout=10000, mail.smtp.host=localhost}])
预期
预期的记录结果:
Expected
Expected logged results:
2018-09-25 12:17:29,155 |↷| EmailNotificationServiceBean#createPayload([SECURE])
2018-09-25 12:17:29,193 |↷| EmailPayloadImpl#<init>([SECURE])
2018-09-25 12:17:29,193 |↷| EmailPayloadImpl#validate([SECURE])
2018-09-25 12:17:29,195 |↷| EmailPayloadImpl#setMailServerSettings([SECURE])
代码
日志方面:
@Aspect
public class LogAspect {
@Pointcut("execution(public @Log( secure = true ) *.new(..))")
public void loggedSecureConstructor() { }
@Pointcut("execution(@Log( secure = true ) * *.*(..))")
public void loggedSecureMethod() { }
@Pointcut("execution(public @Log( secure = false ) *.new(..))")
public void loggedConstructor() { }
@Pointcut("execution(@Log( secure = false ) * *.*(..))")
public void loggedMethod() { }
@Pointcut("execution(* (@Log *) .*(..))")
public void loggedClass() { }
@Around("loggedSecureMethod() || loggedSecureConstructor()")
public Object logSecure(final ProceedingJoinPoint joinPoint) throws Throwable {
return log(joinPoint, true);
}
@Around("loggedMethod() || loggedConstructor() || loggedClass()")
public Object log(final ProceedingJoinPoint joinPoint) throws Throwable {
return log(joinPoint, false);
}
private Object log(final ProceedingJoinPoint joinPoint, boolean secure) throws Throwable {
final Signature signature = joinPoint.getSignature();
final Logger log = getLogger(signature);
final String className = getSimpleClassName(signature);
final String memberName = signature.getName();
final Object[] args = joinPoint.getArgs();
final CharSequence indent = getIndentation();
final String params = secure ? "[SECURE]" : Arrays.deepToString(args);
log.trace("\u21B7| {}{}#{}({})", indent, className, memberName, params);
try {
increaseIndent();
return joinPoint.proceed(args);
} catch (final Throwable t) {
final SourceLocation source = joinPoint.getSourceLocation();
log.warn("\u2717| {}[EXCEPTION {}] {}", indent, source, t.getMessage());
throw t;
} finally {
decreaseIndent();
log.trace("\u21B6| {}{}#{}", indent, className, memberName);
}
}
Log
接口定义:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR})
public @interface Log {
boolean secure() default false;
}
反编译的服务bean:
The decompiled service bean:
@Log
public class EmailNotificationServiceBean
implements EmailNotificationService {
@Log(secure = true)
@Override
public EmailPayload createPayload(Map<String, Object> settings) throws NotificationServiceException {
Map<String, Object> map = settings;
JoinPoint joinPoint = Factory.makeJP((JoinPoint.StaticPart)ajc$tjp_2, (Object)this, (Object)this, map);
Object[] arrobject = new Object[]{this, map, joinPoint};
return (EmailPayload)LogAspect.aspectOf().logSecure(new EmailNotificationServiceBean$AjcClosure7(arrobject).linkClosureAndJoinPoint(69648));
}
有效载荷实现:
@Log
public class EmailPayloadImpl extends AbstractPayload implements EmailPayload {
@Log(secure = true)
public EmailPayloadImpl(final Map<String, Object> settings)
throws NotificationServiceException {
validate(settings, "SMTP connection and credentials");
setMailServerSettings(settings);
}
@Log(secure = true)
private void validate(final Map<String, Object> map, final String message)
throws NotificationServiceException {
if (map == null || map.isEmpty()) {
throwException(message);
}
}
@Log(secure = true)
private void setMailServerSettings(final Map<String, Object> settings) {
this.mailServerSettings = settings;
}
问题
是什么原因:
- 要忽略的
secure = true
构造函数注解属性;和 - 要调用和记录两次的
validate
和setMailServerSettings
方法(一次安全,一次不安全)?
- the
secure = true
constructor annotation attribute to be ignored; and - the
validate
andsetMailServerSettings
methods to be called and logged twice (once securely and once not)?
我怀疑这些问题是相关的.
I suspect the issues are related.
推荐答案
解决方案:
修复重复问题需要调整loggedClass()
切入点定义:
@Pointcut("execution(* (@Log *) .*(..)) && !@annotation(Log)")
public void loggedClass() { }
另请在附加信息部分找到概念证明的链接.
Please also find a link to Proof of concept in the Additional information section.
与连接点相关的问题(由 @Pointcut
注释定义),它们的模式相互交叉 - 这就是日志中重复的原因.
Issue related to join points (defined by @Pointcut
annotation), their patterns cross each other - and this is the reason of duplication in the logs.
在我们的例子中,所有的 @Pointcut
都具有足够的描述性,例如:
In our case all @Pointcut
s named descriptive enough, e.g.:
loggedClass()
涵盖了@Log
注释的类中的所有方法.loggedSecureMethod()
涵盖了所有由@Log(secure = true)
注释的方法.其他的和这个类似,所以我们忽略它们进行解释.
loggedClass()
covers all methods in the classes annotated by@Log
.loggedSecureMethod()
covers all methods annotated by@Log(secure = true)
. Others remain are similar to this one, so let's ignore them for explanation.
因此,如果 EmailPayloadImpl
用 @Log
注释并且 EmailPayloadImpl.validate()
用 @Log(secure= true)
- 我们将有 2 个活动连接点:一个 安全 和一个 非安全.这将导致添加 2 个日志条目.
So in case when EmailPayloadImpl
is annotated with @Log
and EmailPayloadImpl.validate()
is annotated with @Log(secure = true)
- we will have 2 active join points: one secure and one non-secure. And this will cause adding 2 log entries.
假设我们想在注解中引入优先级,即方法级注解应该覆盖类级注解 - 最简单的方法就是避免交叉连接点模式.
Assuming that we want to introduce priority in the annotations, i.e. method-level annotation should overwrite class-level one - the simplest way will be just to avoid crossing join point patterns.
所以我们需要有 3 个方法组:
So we will need to have 3 groups for methods:
- 使用
@Log(secure = true)
=loggedSecureMethod()
注释的方法 - 使用
@Log
=loggedMethod()
注释的方法 没有
@Log
注释的方法,但是在一个用@Log
注释的类中,即:
- Methods annotated with
@Log(secure = true)
=loggedSecureMethod()
- Methods annotated with
@Log
=loggedMethod()
Methods without
@Log
annotation, but within a class annotated with@Log
, which is:
@Pointcut("execution(* (@Log *) .*(..)) && !@annotation(Log)")
public void loggedClass() { }
<小时>
附加信息:
- 如果还需要在类级别处理
@Log(secure = true)
- 需要添加类似于loggedClass()
的附加连接点当然. - 在 GitHub概念证明 >>>
- In case it will be needed to handle also
@Log(secure = true)
on the class level - need to add additional join point similar tologgedClass()
of course. - Added Proof of concept >> in the GitHub
这篇关于各种切入点表达式范围意外触发多个通知调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!