在 GIN 旁边使用 GUICE
我在我的 GWT 项目上成功使用了 GIN。现在我的 RPC 服务将需要来自其他层的数据(业务逻辑 --> 数据访问),在过去我会使用所需的方法来完成单例服务。 像这样的东西 MyGwtServiceImpl.getData() --> MyServerSideService.getData() --> MyDaoGetDataFromDB()
但现在单例并没有得到真正的重视,我想出于测试目的使用注入方法。
我知道我需要知道的是为服务器端安装 GUICE,但我不太确定应该在哪里创建注入器。通常它会在 main 方法中完成,但这里我使用 GWT。
GIN 和 GUICE lib 会冲突吗?
我应该把注射器创建放在哪里?在 onModuleLoad 中?我不确定,因为我的申请将分为几个。
感谢您的帮助!
总结:
- 按照 Thomas 给出的教程(答案)
但是为了注入工作,不要忘记在 web.xml 中替换(实际上是删除):
; MyService com.myapp.MyServiceImpl MyService /com.myapp/MyService 通过 Guice 模块中:
serve("/com.myapp/MyService").with(MyServiceImpl.class);
I'm using successfully GIN on my GWT projet. Now my RPC service will need data from other layer (Business Logic --> Data Access), in the old time I would have done singleton services with needed methods.
Something like
MyGwtServiceImpl.getData() --> MyServerSideService.getData() --> MyDaoGetDataFromDB()
But nowadays singleton are not really appreciated and I would like for testing purpose use the injection method.
I know all I need to know is install GUICE for server side, but I'm not really sure where the creation of the injector should be done. Usually it would be done in a main method, but here I'm using GWT.
Will there be conflict between GIN and GUICE lib?
Where should I put the injector creation ? in the onModuleLoad ? I'm not sure since my application will be divided in several.
Thanks for helping!
Summary :
- Follow the tutorial given by Thomas (The answer)
But for injection to work, don't forget to replace (delete in fact) in web.xml :
<servlet> <servlet-name>MyService</servlet-name> <servlet-class>com.myapp.MyServiceImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyService</servlet-name> <url-pattern>/com.myapp/MyService</url-pattern> </servlet-mapping>
By in a Guice module :
serve("/com.myapp/MyService").with(MyServiceImpl.class);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 ServletContextListener 中创建 Injector。查看 http://code.google.com/p/google-guice /wiki/ServletModule
GIN(Guice for GWT,在客户端)和 Guice(在服务器端)之间绝对没有冲突。
You'd want to create your Injector in a
ServletContextListener
. Have a look at http://code.google.com/p/google-guice/wiki/ServletModuleThere's absolutely no conflict between GIN (Guice for GWT, on the client-side) and Guice (on the server-side).