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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.