如何在Dropwizard v1.1.0中使用UnitOfWorkAwareProxyFactory

如何在Dropwizard v1.1.0中使用UnitOfWorkAwareProxyFactory,dropwizard,Dropwizard,我需要在dropwizard中调用资源外部的DAO方法。 看了这本手册,我不清楚如何使用它。手册上说 SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory()); ExampleAuthenticator exampleAuthenticator = new UnitOfWorkAwareProxyFactory(hibernateBundle) .create(ExampleAuthen

我需要在dropwizard中调用资源外部的DAO方法。 看了这本手册,我不清楚如何使用它。手册上说

SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory());
ExampleAuthenticator exampleAuthenticator = new
    UnitOfWorkAwareProxyFactory(hibernateBundle)
           .create(ExampleAuthenticator.class, SessionDao.class, dao);
谁能告诉我调用DAO的
exampleAuthenticator
方法的用法吗


谢谢,Kedar每个Dropwizard模块都有一个测试套件。以下是您正在寻找的答案:

逻辑是:

  • DAO
    对象实例持有对Hibernate SessionFactory的引用
  • 访问DB的方法调用
    sessionFactory.getCurrentSession()
    。示例代码正在执行本机查询,如果从数据库返回至少一个结果行,则返回true
  • OAuthAuthenticator
    实例持有对
    DAO
    实例的引用,并调用
    DAO
    的适当方法
  • 测试用例如下:

    工作解决方案

    /** initializing proxy dao for authorization */
    AuthenticatorDAOProxy authenticatorDAOProxy = new UnitOfWorkAwareProxyFactory(hibernateBundle)
                .create(AuthenticatorDAOProxy.class, DeviceDAO.class, deviceDAO);
    
    我们现在可以在泽西岛资源外部使用
    authenticatorDAOProxy

    需要注意的是,
    AuthenticatorDAOProxy
    应该有一个接受
    DeviceDAO

    现在您的代理将看起来像

    public class AuthenticatorDAOProxy {
    
      private DeviceDAO deviceDAO;
    
      public AuthenticatorDAOProxy(DeviceDAO deviceDAO) {
        this.deviceDAO = deviceDAO;
      }
    
      @UnitOfWork
      public Boolean checkIsDeviceValid(String deviceId, User user) {
        Device device = deviceDAO.getByDeviceIdAndUser(deviceId, user);
        if (device != null && device.getIsActive() == true) {
            return true;
        }
        return false;
      }
    }
    

    我相信您不需要
    AuthenticatorDAOProxy
    让构造函数接受
    DeviceDAO
    unitofworkawarreproxyfactory
    中的
    create
    方法有几个重载;其中一个会让你写
    。如果没有可用的参数构造函数,则创建(AuthenticatorDAOProxy.class)