开发者

GWT Guice/Gin on the server side problem

开发者 https://www.devze.com 2023-03-10 09:34 出处:网络
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.

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
开发者_Go百科    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.


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".


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

0

精彩评论

暂无评论...
验证码 换一张
取 消