gpt4 book ai didi

java - @Loggable 注解只对某些方法有效

转载 作者:行者123 更新时间:2023-11-30 08:06:45 31 4
gpt4 key购买 nike

我有以下界面:

/**
* Annotation for methods, whose execution should be logged.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Loggable {

/**
* Log severity level of 'before' and 'after' log statements.
*/
enum Level {
DEBUG,
INFO,
WARN,
ERROR
}

/**
* Defines the severity which should be used when logging the method arguments.
*/
Level level() default Level.FATAL;
}

我还有以下类(class):

/**
* Class for logging input and output parameters of any method with annotation @Loggable.
*/
@Aspect
public final class LoggingAspect {
private final Logger log = LoggerFactory.getLogger(getClass());

/**
* @param jp - ProceedingJointPoint
* @param loggable - Loggable
* @return returns the next executable point to proceed in target
* @throws Throwable - throws exception when proceeding with joint point
*/
@Around("execution(* *(..)) && @annotation(loggable)")
public Object loggingAroundMethod(@Nonnull final ProceedingJoinPoint jp,
@Nonnull final Loggable loggable) throws Throwable {
final String signature = jp.getTarget().getClass().getName() + '.' + jp.getSignature().getName();
final List<Object> arguments = Arrays.asList(jp.getArgs());

final Object result;
try {
doLog(loggable.level(), "[BEFORE] {}{}", signature, arguments);
result = jp.proceed();
doLog(loggable.level(), "[AFTER] {}{} result={}", signature, arguments, result);
} catch (Exception e) {
log.error("[AFTER] {}{} exception={}", signature, arguments, e);
throw e;
}

return result;
}

/**
* Logs the message with appropriate log level.
* @param level - level to log
* @param format - format for logging
* @param arguments - arguments for logging
*/
private void doLog(@Nonnull final Loggable.Level level, @Nonnull final String format, final Object... arguments) {
switch (level) {
case DEBUG:
log.debug(format, arguments);
return;

case INFO:
log.info(format, arguments);
return;

case WARN:
log.warn(format, arguments);
return;

case ERROR:
break;

default:
log.error("Unable to appropriately handle given log level={}", level);
}
log.error(format, arguments);
}
}

这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="no">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean class="path.to.my.package.LoggingAspect" />
</beans>

现在,当我将 @Loggable 注释添加到在我的程序的其他地方调用的现有方法时,一切都按预期正确显示在我的日志中。带有方法的工作注释看起来像这样:

@Loggable
public boolean testString(String test) {
return test.equals("foo");
}

但是,当我尝试将注释添加到辅助方法而不是程序中已调用的方法时,没有显示任何日志。所以现在不起作用的代码看起来像这样:

public boolean testString(String test) {
return testStringHelper(test);
}

@Loggable
public boolean testStringHelper(String test) {
return test.equals("foo");
}

谁能深入了解为什么第一个场景有效,而第二个场景使用辅助方法却不行?顺便说一句,辅助方法都是公开的。此外,如果我在辅助方法中添加常规日志语句,它会显示在我的日志中。由于某种原因,这只是注释不适用于辅助方法。

最佳答案

Spring 只能建议已注入(inject)到其他 Spring bean 中的 Spring bean 的方法。如果一个 bean 调用它自己的方法之一,那么通知将不会被执行。

Spring AOP 代理在 docs 中有解释。 .

关于java - @Loggable 注解只对某些方法有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34147603/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com