java区间表达式,java-各种切入点表达式范围意外触发多个建...

博客内容涉及一个日志记录的问题,其中@Log注解的方法和构造函数在日志输出中出现异常行为。具体表现为secure=true的构造函数注解被忽略,且某些方法如validate和setMailServerSettings被记录两次,一次带secure标志,一次不带。问题可能与AOP切面和注解处理有关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

使用方面记录项目,以使所有带有@Log批注标记的方法,类和构造函数均具有写入日志文件的信息.

问题

方法似乎被递归地一层深地调用,但是代码没有显示任何此类递归关系.

实际

记录结果:

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#([{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}])

预期

预期的记录结果:

2018-09-25 12:17:29,155 |?| EmailNotificationServiceBean#createPayload([SECURE])

2018-09-25 12:17:29,193 |?| EmailPayloadImpl#([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("↷| {}{}#{}({})", indent, className, memberName, params);

try {

increaseIndent();

return joinPoint.proceed(args);

} catch (final Throwable t) {

final SourceLocation source = joinPoint.getSourceLocation();

log.warn("✗| {}[EXCEPTION {}] {}", indent, source, t.getMessage());

throw t;

} finally {

decreaseIndent();

log.trace("↶| {}{}#{}", indent, className, memberName);

}

}

日志接口定义:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR})

public @interface Log {

boolean secure() default false;

}

反编译的服务bean:

@Log

public class EmailNotificationServiceBean

implements EmailNotificationService {

@Log(secure = true)

@Override

public EmailPayload createPayload(Map settings) throws NotificationServiceException {

Map 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 settings)

throws NotificationServiceException {

validate(settings, "SMTP connection and credentials");

setMailServerSettings(settings);

}

@Log(secure = true)

private void validate(final Map map, final String message)

throws NotificationServiceException {

if (map == null || map.isEmpty()) {

throwException(message);

}

}

@Log(secure = true)

private void setMailServerSettings(final Map settings) {

this.mailServerSettings = settings;

}

是什么原因造成的:

> secure = true构造函数注释属性将被忽略;和

> validate和setMailServerSettings方法被调用并记录两次(一次安全地登录一次,一次不登录)?

我怀疑这些问题有关.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值