Version 30

    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.

    @Name("myGuicyComponent")
    @Guice public class MyGuicyComponent
    {
       @Inject MyObject myObject;
       ...
    } 
    

     

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

     

    Configuration

     

    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="http://jboss.com/products/seam/components" ...
                           xmlns:guice="http://jboss.org/jbosslabs/seam-guice">
       ...
       <guice:init injector="#{myGuiceInjector}"/>
       ...
    </components> 
    

     

    where 'myGuiceInjector' is your injector (a Seam component that implements com.google.inject.Injector).

     

    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:

     

    @Name("myGuiceInjectorFactory")
    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">
            <guice:modules>
               <value>com.example.guice.GuiceModule1</value>
               <value>com.example.guice.GuiceModule2</value>
            </guice:modules>
    </guice:injector>
    

     

    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:

     

    @Name("myGuicyComponent")
    @Guice("myGuiceInjector")
    public class MyGuicyComponent
    {
       @Inject MyObject myObject;
       ...
    } 
    

     

    FAQ

     

    • 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.

     

    Feedback

     

    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.

     

    Acknowledgment

     

    Many thanks to Tomek for making me Guice addict

     

    Downloads and Sources

     

    You can download the seam-guice.jar here: http://www.jboss.org/shotoku/downloads/other.

     

    Source code is available in the JBoss Labs repository.

     

    XML Schema for configuraion: http://www.jboss.org/file-access/default/members/jbosslabs/freezone/seam-guice/guice-2.1.xsd

     

    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.