2011-06-03 59 views
1

我在我的GWT应用程序中有这样一个脏代码,我的服务层的一些类取决于一个HttpSession对象。因此,例如,在我的DAO之一(这是一个GWT-RPC端点),我有这样的事情:如何使用Guice在GWT中的服务层(DAO)类中注入HttpSession对象?

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... 
    } 
} 

的问题是,我目前迁移的代码使用RequestFactory。我的DAO不再是GWT-RPC端点。所以没有必要继续扩展RemoteServiceServlet然后...

你知道我怎么能/注入(可能与Guice)我的依赖到HttpSession对象,知道我的类不再扩展RemoteServiceServlet?

回答

1

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

+0

非常感谢托马斯, 我会尝试,我会让你知道什么时候它的确定 再次,非常感谢您的帮助 – kaffein 2011-06-05 08:14:24

+0

非常感谢托马斯, 它的工作;-) ...这是大 – kaffein 2011-06-08 12:45:12