创建现有对象的@UnitOfWork-aware 代理

Oli*_*ire 5 java guice dropwizard

我有以下课程:

public FooDAO extends AbstractDAO<Foo> { // Dropwizard DAO
  @Inject FooDAO(SessionFactory sf) { super(sf); }
  public void foo() { /* use SessionFactory */ }
}

public class FooService {
  private final FooDAO fooDAO; // Constructor-injected dependency
  @Inject FooService (FooDAO fooDAO) { this.fooDAO = fooDAO; }

  @UnitOfWork
  public void foo() {
    this.fooDAO.foo();
    System.out.println("I went through FooService.foo()");
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,FooService不是资源,因此 Dropwizard 不知道它并且不会自动代理它。然而,Dropwizard 的聪明人做到了,所以我可以通过UnitOfWorkAwareProxyFactory.

我尝试使用拦截器将这些代理提供给 Guice,但我遇到了一个问题,因为UnitOfWorkAwareProxyFactory只创建新实例并且从不让我传递现有对象。新实例的问题是我不知道要给它的参数,因为它们是由 Guice 注入的。

如何创建@UnitOfWork现有对象的感知代理?

这是我迄今为止制作的拦截器:

public class UnitOfWorkModule extends AbstractModule {
  @Override protected void configure() {
    UnitOfWorkInterceptor interceptor = new UnitOfWorkInterceptor();
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(UnitOfWork.class), interceptor);
    requestInjection(interceptor);
  }

  private static class UnitOfWorkInterceptor implements MethodInterceptor { 
    @Inject UnitOfWorkAwareProxyFactory proxyFactory;
    Map<Object, Object> proxies = new IdentityHashMap<>();

    @Override public Object invoke(MethodInvocation mi) throws Throwable {
      Object target = proxies.computeIfAbsent(mi.getThis(), x -> createProxy(mi));
      Method method = mi.getMethod();
      Object[] arguments = mi.getArguments();
      return method.invoke(target, arguments);
    }

    Object createProxy(MethodInvocation mi) {
      // here, what to do? proxyFactory will only provide objects where I pass constructor arguments, but... I don't have those!
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当然,如果 Dropwizard(或 Guice)为我提供了一种更简单的方法,那是什么?

Oli*_*ire 5

从 Dropwizard 1.1 开始:(尚未发布,截至 2016 年 8 月 10 日)

public class UnitOfWorkModule extends AbstractModule {

  @Override
  protected void configure() {
    UnitOfWorkInterceptor interceptor = new UnitOfWorkInterceptor();
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(UnitOfWork.class), interceptor);
    requestInjection(interceptor);
  }

  @Provides
  @Singleton
  UnitOfWorkAwareProxyFactory provideUnitOfWorkAwareProxyFactory(HibernateBundle<AlexandriaConfiguration> hibernateBundle) {
    return new UnitOfWorkAwareProxyFactory(hibernateBundle);
  }

  private static class UnitOfWorkInterceptor implements MethodInterceptor {

    @Inject
    UnitOfWorkAwareProxyFactory proxyFactory;

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
      UnitOfWorkAspect aspect = proxyFactory.newAspect();
      try {
        aspect.beforeStart(mi.getMethod().getAnnotation(UnitOfWork.class));
        Object result = mi.proceed();
        aspect.afterEnd();
        return result;
      } catch (InvocationTargetException e) {
        aspect.onError();
        throw e.getCause();
      } catch (Exception e) {
        aspect.onError();
        throw e;
      } finally {
        aspect.onFinish();
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)