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

@Loggable Annotation only works for some methods

我有以下界面:

/**
 * 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"); 
}

任何人都可以深入了解为什么第一种情况有效,而使用辅助方法的第二种情况却不行吗?顺便说一句,辅助方法都是 public。此外,如果我在辅助方法中添加常规日志语句,它会显示在我的日志中。由于某种原因,这只是注释不适用于辅助方法。

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

Spring AOP 代理在 docs.

中解释

是因为代理!

Spring 研究代理的概念。它不能代理您的内部电话。所以它不会那样工作。

您可以在 Understanding AOP proxies 上阅读 Spring 文档以更详细地了解它。