【发布时间】:2015-12-08 03:43:05
【问题描述】:
我有如下界面:
/**
* 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 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");
}
任何人都可以深入了解为什么第一个场景有效,但使用辅助方法的第二个场景无效吗?顺便说一句,辅助方法都是公开的。此外,如果我在辅助方法中添加常规日志语句,它确实会显示在我的日志中。它只是由于某种原因不适用于辅助方法的注释。
【问题讨论】:
-
“帮助”方法是否没有被记录
private? Spring AOP annotations can't target private methods. -
辅助方法都是公开的
-
Spring AOP 使用代理,只有进入对象的方法调用通过代理,而不是内部方法调用。
标签: java spring aspectj spring-annotations