我正在尝试实现基于AOP的日志记录谷歌 - Guice。我为此使用了MethodInterceptor
,但它不起作用。我在Spring中通过定义切点来使用它。一切工作都很好。基于AOP的登录Guice
基于Spring规范AOP日志 -
@Aspect
public class LoggingAspect {
private static Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
@Around("requiredLog()")
public Object bentoBoxAround(ProceedingJoinPoint proceedingJoinPoint) {
Object returnValue = null;
try {
logger.info("Entered into the method -> " + proceedingJoinPoint.getSignature().toShortString()
+ " and input arguments are -> " + Arrays.asList(proceedingJoinPoint.getArgs()));
returnValue = proceedingJoinPoint.proceed();
logger.info("Method Execution over !! " + proceedingJoinPoint.getSignature().toShortString());
} catch (Throwable e) {
logger.error("Method has an exception " + e.getMessage());
}
return returnValue;
}
@Pointcut("within(org.cal.bento..*)")
public void allRequiredPakageLog() {
}
}
从上面的代码中,我们可以登录org.cal.bento.*
包内的所有类和方法执行。
基于AOP记录吉斯代码 -
public class GuiceLoggingInterceptor implements MethodInterceptor {
private static Logger logger = LoggerFactory
.getLogger(GuiceLoggingInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object returnValue = null;
try {
logger.info("GUICE - Entered into the method -> " + invocation.getMethod().getName()
+ " and input arguments are -> " + Arrays.asList(invocation.getArguments()));
returnValue = invocation.proceed();
logger.info("Method Execution over !! " + invocation.getMethod().getName());
} catch (Throwable e) {
logger.error("GUICE - Method has an exception " + e.getMessage());
}
return returnValue;
}
}
绑定类 -
public class GuiceAopModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.any(), new GuiceLoggingInterceptor());
}
}
我们可以做类似的吉斯用于记录(通过定义为整个基础只是一个方面类记录系统)。我不想修改每个班级。
Refered教程 - https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/
任何帮助将高度赞赏。
“我为此使用了MethodInterceptor,但它不起作用”。什么是预期的输出? – CKing
我想记录包中的所有方法执行。这段代码我使用的很好,但不会锁定日志文件中的任何日志。 –
你的对象是如何创建的?只有在Guice创建你想拦截的所有实例时,Guice方法拦截才有效。 – pandaadb