JBoss Seam and Google Guice


    Usage - @Guice


    The rule is fairly simple - if you want to use Guice injection in your Seam component, annotate it with the @org.jboss.labs.seam.guice.Guice annotation.

    @Guice public class MyGuicyComponent
       @Inject MyObject myObject;


    For all Seam components annotated with the @Guice annotation, Guice injection will be performed automatically.




    To use @Guice annotation in your Seam web application:


    1. Add seam-guice.jar and guice-1.0.jar to the lib directory. If you are using seam-gen-erated project, edit build.xml file to inculde those libraries in your war.


    2. Configure Guice injector that will be used. To do so, add the following changes to the components.xml file:


    <components xmlns="" ...
       <guice:init injector="#{myGuiceInjector}"/>


    where 'myGuiceInjector' is your injector (a Seam component that implements


    Configuration steps for the Seam ear application should be quite similar.


    Creating Injector


    Since injector is defined with EL expression, you can obtain it in whatever way you like. You can use an injector that is already used in other, non-Seam parts of you application. You may use Seam factory component pattern to provide injector:


    public InjectorFactory
       @Factory(name="myGuiceInjector", scope=APPLICATION, create=true)
       public Injector getInjector()
          // Your code that returns injector    


    On the other hand, if you would like to create a new injector from a list of modules, we provide a convenient way to do this:

    <guice:injector name="myGuiceInjector">


    Multiple Injectors


    By default, an injector configured in the configuration.xml file is used, but if you really need to use multiple injectors (AFAIK, you should use multiple modules instead), you can specify different injector for every Seam component:


    public class MyGuicyComponent
       @Inject MyObject myObject;




    • Q: I got java.lang.ClassNotFoundException: No ClassLoaders found for: com.example.MyModule exception.

    • A: This happens when you reference hot deployed class from the components.xml file. Make sure that your Guice  class is not in the WEB-INF/dev direcotry.


    • Q: I got NPE on the filed annotated with @Inject

    • A: Make sure seam-guice.jar is in the WEB-INF/lib directory of your war.




    This is one of the first versions of the library so let me know in case you find a bug. Your feedback is much appreciated. I usually watch Seam Forum or you can contact me (pawel . wrzeszcz [at] gmail . com) directly.




    Many thanks to Tomek for making me Guice addict


    Downloads and Sources


    You can download the seam-guice.jar here:


    Source code is available in the JBoss Labs repository.


    XML Schema for configuraion:


    Seam and Guice integration is also available as a patch to JBoss Seam project. You can vote to make it part of the Seam


    Other projects integrating Guice


    Blog entry on Seam and Guice integration


    The library has been tested with JBoss Seam 2.1.0.A1.