GWT Guice/Gin 服务器端问题

发布于 2024-11-13 09:03:26 字数 2004 浏览 4 评论 0原文

大家好,我的问题。一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2024-11-20 09:03:26

您必须在 SampleWebModule 中映射您的 WebchargeServiceImpl servlet,而不是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".

感性不性感 2024-11-20 09:03:26

您的 servlet 如何实例化?您是否安装了 Guice Servlet 过滤器

How do your servlets get instantiated? Did you install the Guice Servlet Filter?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文