frjaeger220 / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

ServletContext is null #348

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When using the @Provides annotation on a method in my module extending
ServletModule and adding the servletContext as method parameter (JIT
binding), I'm getting a null for the ServletContext.

@Provides @Singleton
    protected Configuration buildFreemarkerConfig(ServletContext
servletContext, Text2HtmlMethod text2HtmlMethod, ...)

Could this be due to ServletContext being bound to a WeakReference and the
GC collecting it before the JIT binding is evaluated?

Original issue reported on code.google.com by mathias....@gmail.com on 19 Mar 2009 at 1:43

GoogleCodeExporter commented 9 years ago
Or perhaps the same thing in Development stage?

Original comment by mathias....@gmail.com on 19 Mar 2009 at 2:24

GoogleCodeExporter commented 9 years ago
Hmm.

1) null returned by binding at
com.google.inject.servlet.InternalServletModule.configure(InternalServletModule.
java:71)
 but parameter 0 of
com.processtrend.frontend.WebModule$FreeMarkerConfigurationProvider.<init>() is 
not
@Nullable
  while locating javax.servlet.ServletContext
    for parameter 0 at
com.processtrend.frontend.WebModule$FreeMarkerConfigurationProvider.<init>(WebMo
dule.java:127)

Original comment by mathias....@gmail.com on 19 Mar 2009 at 4:23

GoogleCodeExporter commented 9 years ago
Can you test with your debugger to see if GuiceFilter has been initialized? You 
won't be able to access the 
ServletContext binding until that's happened...

Original comment by limpbizkit on 19 Mar 2009 at 5:35

GoogleCodeExporter commented 9 years ago
Ok I feel a stupid. Indeed, it seems that was the problem.

If GuiceServletContextListener would bind the servletContext, the problem would 
be
solved.

Original comment by mathias....@gmail.com on 19 Mar 2009 at 6:12

GoogleCodeExporter commented 9 years ago
Solution:

The InternalServletModule's ServletContext provider should first try
GuiceServletContextListener that has a static WeakReference to the 
ServletContext. If
it's null, go with GuiceFilter's one. I'll create a patch.

Original comment by mathias....@gmail.com on 19 Mar 2009 at 6:21

GoogleCodeExporter commented 9 years ago

Original comment by mathias....@gmail.com on 19 Mar 2009 at 6:27

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks Mathias, I've added a slightly different fix for this in rev 909. Please 
let me know if this works for you and 
thanks for this suggestion.

Original comment by dha...@gmail.com on 21 Mar 2009 at 10:35

GoogleCodeExporter commented 9 years ago
Your fix is ok, but wouldn't it be handier if it's set before the getInjector() 
call?

Original comment by mathias....@gmail.com on 21 Mar 2009 at 5:06

GoogleCodeExporter commented 9 years ago
Dhanji?

Original comment by mathias....@gmail.com on 26 Mar 2009 at 5:34

GoogleCodeExporter commented 9 years ago
Are you using it in an eager singleton or something?

Original comment by dha...@gmail.com on 26 Mar 2009 at 11:25

GoogleCodeExporter commented 9 years ago
Indeed. I'm initializing my FreeMarker context upon startup, and it needs a 
ServletContext to preload/parse 
templates.

Original comment by mathias....@gmail.com on 26 Mar 2009 at 11:51

GoogleCodeExporter commented 9 years ago
Done.

Original comment by dha...@gmail.com on 27 Mar 2009 at 12:41