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

时间:2016-07-08 08:05:09

标签: 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()");
  }
}

现在,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!
    }
  }
}

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

1 个答案:

答案 0 :(得分:4)

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