SeamFramework.orgCommunity Documentation

Chapter 28. Guice integration

28.1. Creating a hybrid Seam-Guice component
28.2. Configuring an injector
28.3. Using multiple injectors

Google Guice is a library that provides lightweight dependency injection through type-safe resolution. The Guice integration (part of the Seam IoC module) allows use of Guice injection for all Seam components annotated with the @Guice annotation. In addition to the regular bijection that Seam performs (which becomes optional), Seam also delegates to known Guice injectors to satisfy the dependencies of the component. Guice may be useful to tie non-Seam parts of large or legacy applications together with Seam.


The Guice integration is bundled in the jboss-seam-ioc library. This dependency is required for all integration techniques covered in this chapter. You will also need the Guice JAR file on the classpath.

The goal is to create a hybrid Seam-Guice component. The rule for how to do this is very simple. If you want to use Guice injection in your Seam component, annotate it with the @Guice annotation (after importing the type org.jboss.seam.ioc.guice.Guice).


@Guice public class MyGuicyComponent
   @Inject MyObject myObject;
   @Inject @Special MyObject mySpecialObject;

This Guice injection will happen on every method call, just like with bijection. Guice injects based on type and binding. To satisfy the dependencies in the previous example, you might have bound the following implementations in a Guice module, where @Special is an annotation you define in your application.

public class MyGuicyModule implements Module

   public void configure(Binder binder)
         .toInstance(new MyObject("regular"));
         .toInstance(new MyObject("special"));

Great, but which Guice injector will be used to inject the dependencies? Well, you need to perform some setup first.

You tell Seam which Guice injector to use by hooking it into the injection property of the Guice initialization component in the Seam component descriptor (components.xml):

<components xmlns=""

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


myGuiceInjector must resolve to a Seam component that implements the Guice Injector interface.

Having to create an injector is boiler-plate code, though. What you really want to be able to do is simply hook up Seam to your Guice modules. Fortunately, there is a built-in Seam component that implements the Injector interface to do exactly that. You can configure it in the Seam component descriptor with this additional stanza.

<guice:injector name="myGuiceInjector">

Of course you can also use an injector that is already used in other, possibly non-Seam part of you application. That's one of the main motivations for creating this integration. Since the injector is defined with EL expression, you can obtain it in whatever way you like. For instance, you may use the Seam factory component pattern to provide injector.


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

By default, an injector configured in the Seam component descriptor is used. If you really need to use multiple injectors (AFAIK, you should use multiple modules instead), you can specify different injector for every Seam component in the @Guice annotation.


public class MyGuicyComponent
   @Inject MyObject myObject;

That's all there is to it! Check out the guice example in the Seam distribution to see the Seam Guice integration in action!