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

2024-04-22

我有以下课程:

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

Now, 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)为我提供了一种更简单的方法,那么它是什么呢?


从 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();
      }
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建现有对象的 @UnitOfWork 感知代理 的相关文章

随机推荐