Guice 中基于 AOP 的日志记录
AOP based logging in Guice
我正尝试在 Google - Guice 中实现基于 AOP 的日志记录。我为此使用了 MethodInterceptor
但它不起作用。我通过定义切入点在 Spring 中使用了相同的方法。那里一切正常。
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() {
}
}
从上面的代码我们可以记录所有 class 和 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;
}
}
绑定Class-
public class GuiceAopModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.any(), new GuiceLoggingInterceptor());
}
}
我们能否在 Guice 中为日志记录做类似的事情(通过为整个日志系统定义一个基于 class 的方面)。我不想修改每个 class.
参考教程 - https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/
非常感谢任何帮助。
您的问题似乎是您没有使用 guice 进行创建。来自 guice 文档:
This approach imposes limits on what classes and methods can be
intercepted:
[...]
Instances must be created by Guice by an @Inject-annotated or
no-argument constructor It is not possible to use method interception
on instances that aren't constructed by Guice.
所以这意味着,因为您的实例是由 spring 创建并可能添加到 guice,guice 没有机会代理那些 类 进行拦截。
来源:
https://github.com/google/guice/wiki/AOP
编辑:
你可以做什么(作为解决方法)来完成这项工作:
Spring 创建您的实例。
把它们写进 guice
创建一个由Guice创建的delegate对象,将(1)的bean注入wrapper。
使用包装器而不是 1 中的对象,然后方法将被拦截。
我正尝试在 Google - Guice 中实现基于 AOP 的日志记录。我为此使用了 MethodInterceptor
但它不起作用。我通过定义切入点在 Spring 中使用了相同的方法。那里一切正常。
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() {
}
}
从上面的代码我们可以记录所有 class 和 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;
}
}
绑定Class-
public class GuiceAopModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.any(), new GuiceLoggingInterceptor());
}
}
我们能否在 Guice 中为日志记录做类似的事情(通过为整个日志系统定义一个基于 class 的方面)。我不想修改每个 class.
参考教程 - https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/
非常感谢任何帮助。
您的问题似乎是您没有使用 guice 进行创建。来自 guice 文档:
This approach imposes limits on what classes and methods can be intercepted:
[...]
Instances must be created by Guice by an @Inject-annotated or no-argument constructor It is not possible to use method interception on instances that aren't constructed by Guice.
所以这意味着,因为您的实例是由 spring 创建并可能添加到 guice,guice 没有机会代理那些 类 进行拦截。
来源:
https://github.com/google/guice/wiki/AOP
编辑:
你可以做什么(作为解决方法)来完成这项工作:
Spring 创建您的实例。
把它们写进 guice
创建一个由Guice创建的delegate对象,将(1)的bean注入wrapper。
使用包装器而不是 1 中的对象,然后方法将被拦截。