Skip to main content

Guice3 Singleton is never instantiated in GAE project



I'm new to Guice and already stuck :)





I pretty much copied classes GuiceConfig, OfyFactory and slightly modified Ofy from Motomapia project (which you can browse) using it as s sample.





I created GuiceServletContextListener which looks like this







public class GuiceConfig extends GuiceServletContextListener

{

static class CourierServletModule extends ServletModule

{

@Override

protected void configureServlets()

{

filter("/*").through(AsyncCacheFilter.class);

}

}



public static class CourierModule extends AbstractModule

{

@Override

protected void configure()

{

// External things that don't have Guice annotations

bind(AsyncCacheFilter.class).in(Singleton.class);

}



@Provides

@RequestScoped

Ofy provideOfy(OfyFactory fact)

{

return fact.begin();

}

}



@Override

public void contextInitialized(ServletContextEvent servletContextEvent)

{

super.contextInitialized(servletContextEvent);

}



@Override

protected Injector getInjector()

{

return Guice.createInjector(new CourierServletModule(), new CourierModule());

}

}







I added this listener into my web.xml







<web-app>

<listener>

<listener-class>com.mine.courierApp.server.GuiceConfig</listener-class>

</listener>



<!-- GUICE -->

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

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

<dispatcher>INCLUDE</dispatcher>

</filter-mapping>



<!-- My test servlet -->

<servlet>

<servlet-name>TestServlet</servlet-name>

<servlet-class>com.mine.courierApp.server.TestServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>TestServlet</servlet-name>

<url-pattern>/test</url-pattern>

</servlet-mapping>

</web-app>







OfyFactory looks like this







@Singleton

public class OfyFactory extends ObjectifyFactory

{

Injector injector;



@Inject

public OfyFactory(Injector injector)

{

this.injector = injector;



register(Pizza.class);

register(Ingredient.class);

}



@Override

public <T> T construct(Class<T> type)

{

return injector.getInstance(type);

}



@Override

public Ofy begin()

{

return new Ofy(super.begin());

}

}







Ofy doesn't have any Guice annotations at all...







public class Ofy extends ObjectifyWrapper<Ofy, OfyFactory>

{

// bunch of helper methods here

}







And finally test servlet where I'm trying to use injected field looks like this







public class TestServlet extends HttpServlet

{

@Inject Ofy ofy;



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

ofy.save(new Pizza());

}

}







Ofy ofy is always null. It's never injected. And it's not injected because OfyFactory is never instantiated, its constructor is never called.





Could you please point what I'm doing wrong? Why my singleton is never created?





Thanks a lot.


Comments

  1. Instead of defining TestServlet in the web.xml file, try deleting its mapping from web.xml and adding this line in the configureServlets() method:

    serve("/test").with(TestServlet.class);


    You may also need to bind TestServlet as a Singleton either by annotating the class with @Singleton or by adding a

    bind(TestServlet.class).in(Singleton.class);


    line to one of the modules.

    What's happening is that Guice is not actually creating your servlet so it isn't able to inject the Ofy object. Guice will only create servlets if it is instructed to do so using a serve(...).with(...) binding. Any servlets defined in the web.xml are outside of Guice's control.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex