Wicket gets Guicy

Having refactored our proxy support in Wicket’s Spring module into a new wicket-ioc module last night, I decided to see how hard it would be to add decent integration for Google’s Guice IoC framework.

Turns out, it’s not very hard at all, as I’ve just done it over my lunch-break. ;-)

There is now a wicket-guice project in Wicket trunk, which does the appropriate stuff. There is also some example code, which should soon show up as a live demo here on the Wicket examples web site.


To integrate Guice into your Wicket application, follow these three simple steps:

Add the wicket-guice module to your build. For example, with Maven 2:

<dependency>
    <groupId>org.apache.wicket</groupId>
    <artifactId>wicket-guice</artifactId>
    <version>1.3.0-incubating-SNAPSHOT</version>
</dependency>

Tell Wicket you want Guice injection for your components, and give it some modules to use (or an entire Injector if you prefer):

public class MyApplication extends WebApplication {
    protected void init() {
        addComponentInstantiationListener(
            new GuiceComponentInjector(this, getModule())
        );
    }
 
    private Module getModule() {
        return new Module() {
            public void configure(Binder binder) {
                binder.bind(IService.class).to(Service.class);
            }
        };
    }
}

Annotate your Components for injection:

public class MyForm extends Form
{
    @Inject
    private IService service;
 
    // ...
    public void onSubmit() {
        service.doFoo();
    }
}

That’s all there is to it.

Coming soon is the ability to pull your Application out of a Guice Module, so you can use constructor injection for it. However, you can always implement this yourself by creating your own IWebApplicationFactory which creates a Guice Injector and pulls the WebApplication out of that, then wires both the application and the Injector into the GuiceComponentInjector constructor.

Constructor injection will be a little tricky to implement, given how we want people to be able to just go “new FooComponent()”. It might be possible to write a custom classloader that rewrites the constructor to do crazy things, but I just haven’t thought about it sufficiently hard quite yet. Even if it’s possible, it will be complicated. I the meantime, it would be fairly simple to weave in an aspect that injects your objects via AspectJ.

11 thoughts on “Wicket gets Guicy

  1. Pingback: A Wicket Diary» Blog Archive » Guice’s start flowing

  2. Pingback: Jean-Baptiste Quenot: Guice: managing dependencies without a single line of XML | Server software

  3. Pingback: Wicket integrates with Guice « Chillenious!

  4. Joseph

    Great news !

    This integration is really nice and simple, that’s great. But it just sums up this whole wicket experience isn’t it ?

    Cheers
    Joseph

    Reply
  5. Pingback: Confluence: codesmell

  6. Pingback: It’s Only Software » Blog Archive » Guice Impressions from a Spring Veteran

  7. Pingback: John Banana Qwerty

  8. Pingback: John Banana Qwerty » Blog Archive » Guice: managing dependencies without a single line of XML

  9. Pingback: Wicket-Guice and Warp-Persist | Richards Blog

  10. Pingback: Die Ruhe Liegt Im Eichenhain » Wicket becomes Guicelicious

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>