2

I'm trying to use Guice to inject properties of a JSF managed bean. This is all running on Google App Engine (which may or may not be important)

I've followed the instructions here:

http://code.google.com/docreader/#p=google-guice&s=google-guice&t=GoogleAppEngine

One problem is in the first step. I can't subclass the Servlet module and setup my servlet mappings there because Faces is handled by the javax.faces.webapp.FacesServlet which subclasses Servlet, not HttpServlet. So, I tried leaving my servlet configuration in the web.xml file and simply instantiating a new ServletModel() along with my business module when creating the injector in the context listener described in the second step.

Having done all that, along with the web.xml configuration, my managed bean isn't getting any properties injected. The method is as follows

@ManagedBean
@ViewScoped
public class ViewTables implements Serializable
{
    private DataService<Table> service;

    @Inject
    public void setService( DataService<Table> service )
    {
        this.service = service;
    }
    public List<Table> getTables()
    {
        return service.getAll();
    }
}

So, I'm wondering if there is a trick to get Guice injecting into a JSF managed bean? I obviously can't use the constructor injection because JSF needs a no-arg constructor to create the bean.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115

6 Answers6

5

Check the following JSF-Guice integration framework/advice:

http://code.google.com/p/jsf-sugar/

http://notdennisbyrne.blogspot.com/2007/09/integrating-guice-and-jsf.html

http://cagataycivici.wordpress.com/2007/03/26/integrating_guice_and_jsf/

http://snippets.dzone.com/posts/show/7171

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I had seen all those before asking the question, and since none worked out of the box thought I would ask. I tried Guicesf and that sugar thing and neither of them worked. I finally took the code from the snippets.dzone.com link and modified it a touch and it appears to be working. The modification was to move the Injector creation out of getValue, and then send a new ervletModule and my own module to it at creation time. Otherwise it didn't work. I was then able to remove my ServletContextListener from web.xml. Probably more than you wanted to know, but thanks for the help! – digitaljoel Dec 30 '09 at 04:20
  • in that case you should've mentioned "I tried this one, it didn't work _in this way_ / _with this exception_ " ;) – Bozho Dec 30 '09 at 07:52
  • yeah, I should have. Sorry about that. – digitaljoel Dec 30 '09 at 16:38
1

You can also create an HTTP servlet that then simple delegates the request on to a FacesServlet (like a wrapper). This should give you the same effect using Guice Servlet.

  • duh, why didn't I think of that? I might trust that more than my own ElResolver, and may give it a go tomorrow. Thanks! – digitaljoel Dec 30 '09 at 04:20
1

How about this approach, works well for us:

http://uudashr.blogspot.com/2008/12/guicing-jsf-with-guice.html

user401810
  • 11
  • 1
  • How funny, I ended up doing something very similar. http://digitaljoel.wordpress.com/2010/05/01/guice-and-jsf-2/ – digitaljoel Jul 26 '10 at 17:43
  • @digitaljoel is this still the best solution for integrating Guice into JSF? Did you have a chance to test it in production environment? – Daniel Novak Apr 18 '12 at 20:57
  • 1
    @DanielNovak I never got it into a production environment but I never saw problems in my messing around either. I was playing with this stuff on google app engine. I have since left JSF and Guice (I liked Guice) and am on a full Spring stack for my own stuff now. – digitaljoel Apr 18 '12 at 21:02
  • Thank you for your response. I like how Guice integrates with MyBatis and Apache Shiro and allows the use of annotations. But these features use AOP (Guice is bundled with some sort of AOP). The current project needs JSF2. So I haven't found a better mix than Guice + JSF2... – Daniel Novak Apr 18 '12 at 21:35
  • @digitaljoel One question though... You are putting the Guice injector inside a custom InjectionProvider which is used only by JSF. What in case you would have a Jersey (JAX-RS) REST interface in the same web app. You would not be able to use the same Guice injector as in JSF? This is usually implemented as a Servlet filter... (http://jersey.java.net/nonav/apidocs/1.11/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html). I would need to have two Guice injectors in my app then? – Daniel Novak Apr 18 '12 at 22:32
  • @DanielNovak looks that way based on what you are saying but I'm no authority on it. I really only experimented with Guice on my after-hours time in a hobby project, so I learned just enough to be dangerous and apparently enough to get it working for my project with jsf. – digitaljoel Apr 18 '12 at 22:46
0

being the developer of jsf sugar I really would like to know the problem you had using it. We are already using it in production here so there shouldn't be any "show stoppers", maybe something is just not well documented? Just drop me a mail: murbanek(at)gmx_net (replace the _ with a .) .

0

check out http://code.google.com/p/guice2jsf/, and website starchu.blogspot.com, it has excellent library that provides Guice and JSF 2.0 integration

jerry
  • 1
0

As information in this post are getting out of date but the question is still relevant, I'd like to share my findings about this topic. I wrote a little tutorial including a runnable sample project on how to setup a fully guice powered web stack. You can find it here: https://github.com/skuzzle/guice-jsf

SimonT
  • 165
  • 2
  • 11