如何使用Guice在GWT中的服务层(DAO)类中注入HttpSession对象? [英] How to @Inject a HttpSession object in a service layer (DAO) class in GWT using Guice?

查看:128
本文介绍了如何使用Guice在GWT中的服务层(DAO)类中注入HttpSession对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GWT应用程序中有这样一个脏代码,我的服务层的一些类依赖于 HttpSession 对象。例如,在我的一个DAO(这是一个GWT-RPC端点)中,我有类似这样的内容:

  public class MyExampleDAO扩展RemoteServiceServlet {
public findItems(){
//这里我需要获取对象会话以检索当前登录的用户以查询其所有项目...
}
}

问题是,我正在迁移代码以使用RequestFactory。我的DAO不再是GWT-RPC端点。因此,不需要扩展RemoteServiceServlet,然后......



你知道我可以如何获得/注入(可能带有

h2_lin>解决方案

getThreadLocalRequest()。getSession()应该这样做。 RequestFactoryServlet 有一个类似的(但是是静态的) getThreadLocalRequest()方法,你可以从你的服务中访问。
否则,您可以让Guice注入 Provider< HttpSession> ,看看这些项目
https://github.com/mgenov/injecting-request-factory https://github.com/etiennep/injected-requestfactory 了解使用Guice和RequestFactory的一些示例代码。


I have such a dirty code in my GWT app, some of the classes of my service layer depend on an HttpSession object. So for example, in one of my DAO (which was a GWT-RPC endpoint) I have something like this :

public class MyExampleDAO extends RemoteServiceServlet {
   public findItems() {
       // here I need to get the object session to retrieve the currently logged in user in order to query for all its items...
   }
}

The problem is that, I am currently migrating the code to use RequestFactory. My DAO will not be a GWT-RPC endpoint anymore. So no need to extend RemoteServiceServlet then ...

Do you know how I can get/inject (probably with Guice) my dependency to HttpSession Object, knowing that my class does not extend RemoteServiceServlet anymore?

解决方案

getThreadLocalRequest().getSession() should do it. RequestFactoryServlet has a similar (but static) getThreadLocalRequest() method that you can access from your service. Otherwise, you can get Guice to inject a Provider<HttpSession>, have a look at these projects https://github.com/mgenov/injecting-request-factory and https://github.com/etiennep/injected-requestfactory for some sample code using Guice with RequestFactory.

这篇关于如何使用Guice在GWT中的服务层(DAO)类中注入HttpSession对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆