xiaodududu / google-guice

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

Problem using guice-servlet in multiple war, when guice-servlet.jar is in ear's libraries #559

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
We have project, which has several war archives, and all of them are using 
GuiceFilter. For some reasons we need guice-servlet.jar to be placed in ear's 
libraries (APP-INF/lib for weblogic in our case).
There is problem, that only the last deployed war is served, other returns 404.

GuiceServletContextListener sets ServletContext to static variable in 
GuiceFilter, creates an injector and sets the pipeline by static method.

Now, we have to extend GuiceFilter and override init method, which takes 
injector (created and stored by GuiceServletContextListener) from 
servletcontext, inject itself (to have non-static field injectedPipeline 
initialized) and call super.init().

It would be nice to have this feature in GuiceFilter (maybe configurable by 
init parameter).

Original issue reported on code.google.com by stra...@gmail.com on 19 Oct 2010 at 10:25

GoogleCodeExporter commented 9 years ago
Are you using the latest SVN head of guice & guice-servlet?  I believe it 
supports multiple injectors in the same JVM (whereas the version shipped with 
2.0 did not).

Original comment by sberlin on 19 Oct 2010 at 10:32

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
yes, lastest SVN.
doFilter method in GuiceFilter is using 
FilterPipeline filterPipeline = null != injectedPipeline ? injectedPipeline : 
pipeline;

but injectedPipeline is null, because it is not injected. I am getting 
MULTIPLE_INJECTORS_WARNING message in log, and previously store pipeline (which 
is static) is overwritten.

Original comment by stra...@gmail.com on 19 Oct 2010 at 10:44

GoogleCodeExporter commented 9 years ago
Here is simple scenario:
ear
-APP-INF/lib/guice*.jar
-war1
 -/servlet1
-war2
 -/servlet2

both war1 and war2 have in its own web.xml defined GuiceFilter and class which 
extends GuiceServletContextListener to define injector.
So even GuiceFilter is defined as a Singleton, two instances are created (one 
for war1 and one for war2) by container (so they are not injected).
In this case, both instances shares same static servletcontext and same static 
pipeline.

The simplest solution should be imho injecting members in GuiceFilter in init 
method before pipelines are initialized.

Or maybe I am using it wrong way...

Original comment by stra...@gmail.com on 21 Oct 2010 at 1:46

GoogleCodeExporter commented 9 years ago
This is because you're placing it in the common classloader--you need to place 
it in the webapp classloader instead (WEB-INF rather than APP-INF). This is by 
deliberate design in Guice.

Original comment by dha...@gmail.com on 21 Oct 2010 at 11:17

GoogleCodeExporter commented 9 years ago
To clarify, you should place a copy of guice-servlet-*.jar in each WEB-INF/lib 
directory of your app.

You can share all the other guice-*.jars however (in APP-INF/lib), should you 
so desire.

Original comment by dha...@gmail.com on 21 Oct 2010 at 11:19

GoogleCodeExporter commented 9 years ago
If you use Jersey, you have to place both  guice-servlet-*.jar and jersey-guice 
, otherwise...you get this multibings issue.

Iker.

Original comment by iker.ola...@gmail.com on 7 May 2014 at 1:30