如何在Dropwizard v1.1.0中使用UnitOfWorkAwareProxyFactory
我需要在dropwizard中调用资源外部的DAO方法。 看了这本手册,我不清楚如何使用它。手册上说如何在Dropwizard v1.1.0中使用UnitOfWorkAwareProxyFactory,dropwizard,Dropwizard,我需要在dropwizard中调用资源外部的DAO方法。 看了这本手册,我不清楚如何使用它。手册上说 SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory()); ExampleAuthenticator exampleAuthenticator = new UnitOfWorkAwareProxyFactory(hibernateBundle) .create(ExampleAuthen
SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory());
ExampleAuthenticator exampleAuthenticator = new
UnitOfWorkAwareProxyFactory(hibernateBundle)
.create(ExampleAuthenticator.class, SessionDao.class, dao);
谁能告诉我调用DAO的exampleAuthenticator
方法的用法吗
谢谢,Kedar每个Dropwizard模块都有一个测试套件。以下是您正在寻找的答案: 逻辑是:
DAO
对象实例持有对Hibernate SessionFactory的引用李>
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)