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:


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() {
            new GuiceComponentInjector(this, getModule())
    private Module getModule() {
        return new Module() {
            public void configure(Binder binder) {

Annotate your Components for injection:

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

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”