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 is going to 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.

9 Comments so far

  1. […] Our London chapter has implemented Wicket - Guice integration. […]

  2. […] Have you heard about Guice?  It’s a Google Code project that aims to offer a nice and simple dependency injection framework in pure Java, without any line of XML.  I was actually looking for something refreshing like this, as I’m more and more reluctant to integrate Spring in my projects because of the huge XML files to maintain.  I came across Guice on AlMaw’s blog, it’s already integrated into Wicket, and Allastair can already assert to have a user, as I just baked it into Pickwick using a custom IWebApplicationFactory. 21 Jun 07 | Source link | Apache | | On Server Also: […]

  3. […] Wicket integrates with Guice 20 06 2007 Al added a new project for integrating Guice, which is a very elegant Dependency Injection framework. He beat me to it; it had been on my radar for while to do build such an integration project, but I didn’t want to start before I had a good idea how to re-write the phonebook example. […]

  4. Joseph on June 23rd, 2007

    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

  5. Confluence: codesmell on October 3rd, 2007

    Guice & Wicket, a perfect match….

    You know Wicket…

  6. […] it “just worked” as easily as the Wicket - Spring integration does. This is a result of Alastair’s stellar work on the Wicket project - great […]

  7. John Banana Qwerty on December 14th, 2007

    Guice: managing dependencies without a single line of XML…

    Have you heard about Guice? 
    It’s a Google Code project that aims to offer a nice and simple
    dependency injection framework in pure Java, without any line of XML. 
    I was actually looking for something refreshing like this, as I’m more
    and more …

  8. […] integrate Spring in my projects because of the huge XML files to maintain. I came across Guice on AlMaw’s blog, it’s already integrated into Wicket, and Allastair can already assert to have a user, as I […]

  9. […] had already been looking at the wicket-guice project which provides support for Guice in […]

Leave a reply