1

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.

brakebg
  • 414
  • 3
  • 11
  • 24
  • in class WebchargeServiceImpl, the instance executeOperations is null i expect to be init. What do you think ? – brakebg Jun 03 '11 at 13:34

2 Answers2

5

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

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Thanks Thomas, but i did it but still executeOperations instance is null. I have updated the post ,WebchargeServiceImpl is bind in SampleWebModule. Anything else i m missing? – brakebg Jun 06 '11 at 15:55
  • 1
    Let me reiterate: you have to **map** your servlet (not just *bind* the class) from within the `ServletModule` and **not** from your `web.xml`: `serve("/Webcharge/WebchargeService").with(WebchargeServiceImpl.class)`. See http://code.google.com/p/google-guice/wiki/ServletModule#The_Binding_Language – Thomas Broyer Jun 07 '11 at 08:15
1

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

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Yes i have GuiceFilter declared in my web.xml but .. the problem is still ipen. Thanks Peter for comments my post. – brakebg Jun 06 '11 at 15:56