Google Guice and Apache MyFaces 1.2.x

With the advent of Apache MyFaces 1.2.2 it is possible to use Google Guice for managing your backing beans dependencies (see MYFACES-1747).

Dennis blogged about it already before he actually went ahead and created an ELResolver for MyFaces. His solution still needs some work:

  • register the GuiceResolver in your project’s faces-config.xml file
  • create a ServletContextListener to have your modules available

The approach in the is OK so far, but you need to rewrite the Listener for every project. The good news is, that when you use Java6, you can use ServiceLoader class to load the Guice modules (see here as well). So… we could use a more generic ServletContextListener for the “guice support”.

* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
package net.wessendorf.jsf.guice;

import java.util.ServiceLoader;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.myfaces.el.unified.resolver.GuiceResolver;


* <b>This class requires Java6</b><br/>
* <p/>A <code>ServletContextListener</code> to use MyFaces 1.2.2's <code>GuiceResolver</code>.
* <p/>Put your custom Guice Modules to <code>WEB-INF/services/</code>.
* @author Dennis Byrne
* @author Matthias Wessendorf
public class GuiceServletContextListener implements ServletContextListener
public void contextInitialized(ServletContextEvent event)
ServletContext ctx = event.getServletContext();
ServiceLoader<Module> modules = ServiceLoader.load(;
Injector injector = Guice.createInjector(modules);
ctx.setAttribute(GuiceResolver.KEY, injector);

public void contextDestroyed(ServletContextEvent event)
ServletContext ctx = event.getServletContext();

I think I’ll put that class to a SVN folder soon (perhaps the new commons MyFaces module). But so far… you can already enjoy it!

BTW. in Trinidad we have a similar facility like ServiceLoader, once that is factored out to a util, I guess we can make the listener part of MyFaces Core.



Posted in apache, java, jsf, myfaces

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: