GWT Guice/Gin 服务器端问题
大家好,我的问题。一个 GWT 项目,正如我读过的那样,Gin 只能在客户端使用,而 Guice 则可以在服务器端使用。这是我的问题。
首先发布一些示例代码。
服务器端。
public class WebchargeServiceImpl extends RemoteServiceServlet implements WebchargeService
{
@Inject
private Injector injector;
@Inject
private ExecuteOperations executeOperations;
.....
executeOperations.do(); ....
这是注入的类 ExecuteOperations
@Singleton
public class ExecuteOperations
{
.........
}
另外,我有 servlet 模块类
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>
我缺少一些东西,因为我每次都得到 null,这段代码在 servlet/jsp env 中工作正常,但在这里... 咨询处。
谢谢。
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.
Server side.
public class WebchargeServiceImpl extends RemoteServiceServlet implements WebchargeService
{
@Inject
private Injector injector;
@Inject
private ExecuteOperations executeOperations;
.....
executeOperations.do(); ....
Here is the injected class ExecuteOperations
@Singleton
public class ExecuteOperations
{
.........
}
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>
I'm missing something because i get null every time, this code works ok in servlet/jsp env but here...
Advice place.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在
SampleWebModule
中映射您的WebchargeServiceImpl
servlet,而不是在web.xml
中;否则它将由您的 servlet 容器而不是 Guice 构建,因此它不会被“注入”。You have to map your
WebchargeServiceImpl
servlet in yourSampleWebModule
, not in yourweb.xml
; otherwise it'll be constructed by your servlet container and not by Guice, so it won't be "injected".您的 servlet 如何实例化?您是否安装了 Guice Servlet 过滤器?
How do your servlets get instantiated? Did you install the Guice Servlet Filter?