本文介绍了如何在Dropwizard v1.1.0中使用UnitOfWorkAwareProxyFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在dropwizard中的资源外部调用DAO方法.看手册我不清楚如何使用它.手册说
SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory());ExampleAuthenticator exampleAuthenticator =新UnitOfWorkAwareProxyFactory(hibernateBundle).create(ExampleAuthenticator.class,SessionDao.class,dao);
谁能告诉我调用DAO的 exampleAuthenticator
方法的用法.
感谢Kedar
解决方案
工作解决方案
/**初始化代理dao以进行授权*/AuthenticatorDAOProxy authenticatorDAOProxy =新的UnitOfWorkAwareProxyFactory(hibernateBundle).create(AuthenticatorDAOProxy.class,DeviceDAO.class,deviceDAO);
我们现在可以在球衣资源之外使用 authenticatorDAOProxy
需要注意的一件事, AuthenticatorDAOProxy
应该有一个接受 DeviceDAO
现在您的proxyDao会变成
公共类AuthenticatorDAOProxy {私有DeviceDAO deviceDAO;public AuthenticatorDAOProxy(DeviceDAO deviceDAO){this.deviceDAO = deviceDAO;}@UnitOfWorkpublic Boolean checkIsDeviceValid(String deviceId,User user){设备设备= deviceDAO.getByDeviceIdAndUser(deviceId,user);if(device!= null&&device.getIsActive()== true){返回true;}返回false;}}
I need to call DAO methods outside resource in dropwizard.Looking at the manual Im unclear how to use it. The manual says
SessionDao dao = new SessionDao(hibernateBundle.getSessionFactory());
ExampleAuthenticator exampleAuthenticator = new
UnitOfWorkAwareProxyFactory(hibernateBundle)
.create(ExampleAuthenticator.class, SessionDao.class, dao);
Can anyone show me the usage of exampleAuthenticator
methods which call DAO.
Thanks, Kedar
解决方案
Working solution
/** initializing proxy dao for authorization */
AuthenticatorDAOProxy authenticatorDAOProxy = new UnitOfWorkAwareProxyFactory(hibernateBundle)
.create(AuthenticatorDAOProxy.class, DeviceDAO.class, deviceDAO);
We can now use authenticatorDAOProxy
outside jersey resources
One thing to note., AuthenticatorDAOProxy
should have a constructor accepting DeviceDAO
Now your proxyDao will look like
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;
}
}
这篇关于如何在Dropwizard v1.1.0中使用UnitOfWorkAwareProxyFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!