Introducing @Guice annotation
You may already have heard about Seam being a “deep integration framework” and Google Guice providing lightweight dependency injection. The news is that you can now easily use Seam together with Guice.
The WebBeans specification is built upon concepts from both of those frameworks. Since Seam provides Spring support in the IoC module, why not to integrate Guice as well? Thinking this way I have implemented Guice support for Seam.
Quick Howto
The rule is fairly simple – if you want to use Guice injection in your Seam component, annotate it with the @Guice annotation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Name("myGuicyComponent") | |
@Guice | |
public class MyGuicyComponent | |
{ | |
@Inject MyObject myObject | |
} |
For all Seam components annotated with the @Guice annotation, Guice injection will be performed automatically.
Great, but wait… which Guice injector will be used? Well, the one you provide. Just edit components.xml:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<components | |
xmlns="http://jboss.com/products/seam/components" | |
xmlns:guice="http://jboss.org/jbosslabs/seam-guice"> | |
… | |
<guice:init injector="#{myGuiceInjector}/> | |
… | |
</components> |
myGuiceInjector
should be a Seam component that implements the Injector interface.
You may want to create it from a list of modules:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<guice:injector name="myGuiceInjector"> | |
<guice:modules> | |
<value>com.example.guice.GuiceModule1</value> | |
<value>com.example.guice.GuiceModule2</value> | |
</guice:modules> | |
</guice:injector> |
Of course you can also use an injector that is already used in other, possibly non-Seam, parts of you application. That was one of the reasons I implemented this integration – to tie other parts of an application that used Guice together with Seam.
To enable Seam and Guice integration, add seam-guice.jar library to your application. Do not forget to put guice-1.0.jar in as well 😉
Give it a try!
I have included more information on the wiki page. You can download seam-guice library and browse the source code. It is implemented as a simple interceptor. Let me know if you find a bug or a place for improvement.
If you like this solution, you can vote to make it part of the Seam project. You may also want to have a look at Guice and plain JSF integration and the Guice extension to JBoss Microcontainer.
Many thanks to Tomek, who introduced Guice to me, Kamil, for feedback and Adam, for encouraging me to blog about it 😉
-Paweł
Hi Pawel, Nice work you’ve done there. Why don’t you donate your code to the seam base code instead? I think it would be great to have this in seam distribution. 😉
Thanks, Joshua!
Well, I’ve already done it: http://jira.jboss.com/jira/browse/JBSEAM-3044.
Waiting for acceptance 🙂
Cheers, -Paweł
In poland thare is so sunshine and you wrote modules for guice ;p hehe it’s very usefull couple of code 😉 DI is base for cool systems 😉
Niceeee … i am actually doing a presentation next month on Seam and another on IoC comparison (spring vs hivemind vs seam vs guice) … i think I’m gonna have to add this to the presentation if you don’t mind.
@Joseph:
Sure, I’ll be really glad.
If you could share the slides, I’d love to have a look at such a comparison.
Thanks for doing this …
One question if you define the injector as follows, do you still need a seam component that implements the Injector interface?
If so, do you have an example component?
1.
2.
3. com.example.guice.GuiceModule1
4. com.example.guice.GuiceModule2
5.
6.
Thanks !
Hi Rod,
No, you don’t. If you configure guice:injector in your components.xml file, a Seam component will be created automatically. This component implements the Injector interface.
-Pawel
Hi,
we are using jboss but not seam is there a way to integrate guice for e.g. ejb’s ?
-mitch
Hi Mitch,
Yes, you can integrate Guice with EJBs with an interceptor that injects dependencies after a bean is created or before each method call.
Here is a good example: http://anonsvn.labs.jboss.com/labs/jbosslabs/labs-3.0-build/core/core-api/src/main/java/org/jboss/labs/injection/ejb3/LabsInjectionInterceptor.java.
-Pawel