问题描述
我的问题。一个GWT项目,正如我读过的Gin我只能在客户端使用而不是Guice在服务器端使用。这是我的问题。
Hi guys with my question. A GWT project, as i have read Gin i usable only on the client side than Guice is usable on the server side. Here is my question.
首先发布一些示例代码。
Let first post some example code.
服务器端。
public class WebchargeServiceImpl extends RemoteServiceServlet implements WebchargeService
{
@Inject
private Injector injector;
@Inject
private ExecuteOperations executeOperations;
.....
executeOperations.do(); ....
这里是注入的类ExecuteOperations
Here is the injected class ExecuteOperations
@Singleton
public class ExecuteOperations
{
.........
}
我也有servlet模块类
Also i have servlet module class
public class SampleWebGuiceServletConfig extends GuiceServletContextListener
{
@Override
protected Injector getInjector()
{
return Guice.createInjector(Stage.DEVELOPMENT, new SampleWebModule());
}
} // class
...。
public class SampleWebModule extends ServletModule
{
@Override
protected void configureServlets()
{
bind(WebchargeServiceImpl.class); //is it correct to bind a class like that?
} // class
web.xml
<servlet>
<servlet-name>.......WebchargeService</servlet-name>
<servlet-class>.....WebchargeServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>.........WebchargeService</servlet-name>
<url-pattern>/Webcharge/WebchargeService</url-pattern>
</servlet-mapping>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>......SampleWebGuiceServletConfig</listener-class>
</listener>
</web-app>
我丢失了一些东西,因为每次我都为空,因此此代码在servlet / jsp env中可以正常工作但是在这里...
建议地点。
I'm missing something because i get null every time, this code works ok in servlet/jsp env but here...Advice place.
谢谢。
推荐答案
您必须将 WebchargeServiceImpl
servlet映射到 SampleWebModule
中的 not 中您的 web.xml
;否则它将由您的servlet容器而不是Guice构造,因此不会被注入。
You have to map your WebchargeServiceImpl
servlet in your SampleWebModule
, not in your web.xml
; otherwise it'll be constructed by your servlet container and not by Guice, so it won't be "injected".
这篇关于GWT Guice / Gin在服务器端出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!