我遇到切入点的问题,为此,当log.isDebugEnabled为true时,我试图启用@Around
,我正在尝试以下代码:
@Pointcut("within(org.apache.commons.logging.impl.Log4JLogger..*)")
public boolean isDebugEnabled() {
return log.isDebugEnabled();
}
出于测试目的,我配置了两个方面
@AfterThrowing(value = "!isDebugEnabled()",throwing = "exception")
和
@Around(value = "isDebugEnabled()")
但是,每当我尝试执行代码时,总是到@AfterThrowing
,而且我不清楚我在做什么错!
我在Spring MVC 4.3
上使用AspectJWeaver 1.8.9!
这是一个模拟问题的示例类:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Component
@Aspect
public class SampleAspect {
private static final Log log = LogFactory.getLog(SampleAspect.class);
@Pointcut("within(org.apache.commons.logging.impl.Log4JLogger..*)")
public boolean isDebugEnabled() {
return log.isDebugEnabled();
}
@AfterThrowing(value = " !isDebugEnabled()",throwing = "exception")
public void getcalledOnException(JoinPoint joinPoint,Exception exception) {
log.error("Method " + joinPoint.getSignature() + " Throws the exception " + exception.getStackTrace());
}
//Never execute around method even when log.isDebugEnabled() = true
@Around(value = "isDebugEnabled()")
public Object aroundTest(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Object proceed;
try {
proceed = proceedingJoinPoint.proceed();
} catch (Exception e) {
throw e;
}
stopWatch.stop();
log.debug("It took " + stopWatch.getTotalTimeSeconds() + " seconds to be proceed");
return proceed;
}
}
修改, 我尝试使用aspectJ中的if(),但在我的项目中也无法使用。
@Pointcut("call(* *.*(int)) && args(i) && if()")
public static boolean someCallWithIfTest(int i) {
return i > 0;
}
不确定我是否需要添加其他导入,但是我没有设法使其生效。