How to @Inject a HttpSession object in a service layer (DAO) class in GWT using Guice?

烂漫一生 提交于 2020-01-23 03:50:46

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6225542/how-to-inject-a-httpsession-object-in-a-service-layer-dao-class-in-gwt-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!