views:

475

answers:

5

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.

+2  A: 

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%5Fguice%5Fand%5Fjsf/

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

Bozho
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
in that case you should've mentioned "I tried this one, it didn't work _in this way_ / _with this exception_ " ;)
Bozho
yeah, I should have. Sorry about that.
digitaljoel
+1  A: 

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.

Dhanji R Prasanna
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
A: 

Hi digitaljoel, 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 .) .

put this as a comment to his quetion, not as an answer.
Bozho
A: 

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

How about this approach, works well for us:

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

How funny, I ended up doing something very similar.http://digitaljoel.wordpress.com/2010/05/01/guice-and-jsf-2/
digitaljoel