0

我的大部分代码都在使用 google guice 注入,它一直运行良好,直到我制作了 google guice 注释

代码本身是一个jax-rs Web 服务,我在其中传递 2 个标头参数userIdadminId然后从 Oracle 数据库中检索数据。注释的想法是我会计算某些代码段需要多长时间。传递给注释并与类和代码完成所需的userId时间一起注销。

问题是,当我添加此代码时,它会导致其中一个 guice 模块失败,从而使项目中的所有其他 guice 模块都失败。

非常基本的代码

数据库调用调用的最低类

public class lowestClass {
  public static lowestClass create(final String userId){
    final Injector injector = Guice.createInjector(new LowestClassModule(userId));
    return injector.getInstance(lowectClass.class);
  }

  @LogTimerMeasurement
  public DataInfo getDataInfo() {
    //populates a DataInfo object with info from database
  }
}

最低等级模块

public class lowestClassModule {
  public final String adminId;
  public final String userId;

  public lowestClassModule(final String adminId, final String userId){
    this.adminId = adminId;
    this.userId = userId;
  }

  public lowestClassModule(final String userId){
    this.adminId = "NoAdminId";
    this.userId = userId;
  }

  @Override
  protected void configure(){
    install(new TimerBindingModule(userId));
  }
}

上流社会

public class upperClass {
  @LogTimerMeasurement
  public DataResponse getDataResponse(){
    //Calls lower class gets DataInfo object and returns DataResponse object
  }

  //Guice module for endpoint
  public static class Module extends AbstractModule{
    private final String adminId;
    private final String userId;

    public Module(final String userId){
      this.adminId = "NoAdminId";
      this.userId = userId;
    }

    public Module(final String adminId, final String userId){
      this.adminId = adminId;
      this.userId = userId;
    }

    @Override
    protected void configure(){
      install(new TimerBindingModule(userId));
      bind(lowestClass.class).toInstance(lowestClass.create(userId));
    }
  }
}

Guice 注释

注释接口

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogTimerMeasurement {

}

注释拦截器

public class LogTimerMeasurementInterceptor implements MethodInterceptor {
    private final String userId;

    public LogTimerMeasurementInterceptor(final String userId){
        this.userId = userId;
    }

    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        final Method method = invocation.getMethod();
        final String methodName = method.getName();
        final Class<?> declaringClass = method.getDeclaringClass();

        final I18N supplyI18n = I18NFactory.createI18N(this.getClass());

        final long startTime = System.currentTimeMillis();
        try{
            return invocation.proceed();
        }catch (final Exception e){
            supplyI18n.log(getClass() + "timerError", "\n{0} : {1} :: Unable to log time due to {1}",declaringClass + "." + methodName, userId, e);
            return invocation.proceed();
        }finally {
            final long endTime = System.currentTimeMillis();
            supplyI18n.log("timerMessage", "\n{0} : {1} :: Process completed in {2} milliseconds", declaringClass + "." + methodName, userIf, endTime - startTime);
        }
    }
}

注释模块

public class TimerBindingModule extends AbstractModule {
    private final String userId;

    public TimerBindingModule(final String userId) {
        this.userId = userId;
    }

    @Override
    protected void configure() {
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(LogTimerMeasurement.class), LogTimerMeasurementInterceptor.create(userId));
    }
}
4

0 回答 0