【问题标题】:AOP based logging in GuiceGuice 中基于 AOP 的日志记录
【发布时间】:2016-07-25 11:15:10
【问题描述】:

我正在尝试在 Google - Guice 中实现基于 AOP 的日志记录。我为此使用了MethodInterceptor,但它不起作用。我通过定义切入点在 Spring 中使用了相同的方法。那里一切正常。

基于 AOP 的日志记录的 Spring 代码 -

@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 的日志记录的 Guice 代码 -

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());
  }
}

我们可以在 Guice 中做类似的日志记录吗(通过为整个日志记录系统只定义一个基于 Aspect 的类)。我不想修改每个类。

参考教程 - https://schakrap.wordpress.com/2009/07/30/method-entry-exit-logging-in-guice-with-aop/

任何帮助将不胜感激。

【问题讨论】:

  • “我已经为此使用了 MethodInterceptor 但它不起作用”。预期的输出是什么?
  • 我想记录一个包内的所有方法执行。我使用的这段代码很好,但不会锁定日志文件中的任何日志。
  • 您的对象是如何创建的?仅当 Guice 正在创建您要拦截的所有实例时,使用 guice 的方法拦截才有效。
  • 您的日志记录配置可能有问题吗?从根本上说,您的代码应该可以正常工作。
  • @pandaadb,我在我的项目中同时使用 spring 和 guice,spring 为 guice 创建对象。

标签: java logging aop aspectj guice


【解决方案1】:

您的问题似乎是您没有使用 guice 进行创建。来自 guice 文档:

这种方法对可以使用的类和方法施加了限制 截获:

[...]

实例必须由 Guice 通过 @Inject-annotated 或 无参构造函数不能使用方法拦截 在非 Guice 构造的实例上。

所以这意味着,因为您的实例是由 spring 创建的并且可能添加到 guice,所以 guice 没有机会代理这些类进行拦截。

来源:

https://github.com/google/guice/wiki/AOP

编辑:

你可以做的(作为解决方法)能够完成这项工作:

  1. Spring 会创建您的实例。

  2. 将它们放入guice

  3. 创建一个由 Guice 创建的委托对象,并将 (1) 的 bean 注入到包装器中。

  4. 使用包装器而不是 1 中的对象,然后方法将被拦截。

【讨论】:

  • 是的,我也做过这件事,它就像魅力一样。在我的例子中,spring 正在加载所有依赖项,所以我做了一些配置更改。
  • 听起来不错 :) 如果它回答了您的查询,您可以将答案标记为正确
猜你喜欢
  • 1970-01-01
  • 2018-04-13
  • 2012-09-12
  • 2010-09-19
  • 2012-09-30
  • 1970-01-01
  • 2011-04-15
  • 2012-11-04
  • 1970-01-01
相关资源
最近更新 更多