How to integrate Guice 2 into Wicket?
I want to use Guice 2 with Wicket 1.4. There is a "wicket-guice" package, which uses Guice 1. Can someone give me an example h开发者_如何学Goow to configure Wicket to use Guice 2 for injection (with Maven).
As you can see blow, I've found a solution, but I wonder, if it would be better to use Guice Servlets and register the whole Wicket Application as a ServletFilter with Guice. But I think this would conflict with wickets object creation strategy.
To answer myself I post the solution, which I found with the help of AtomicGamer Dev Blog.
Since wicket-guice supports only Guice 1, Guice needs to be excluded from the wicket-guice extension.
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-guice</artifactId>
<version>${wicket.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.code.guice</groupId>
<artifactId>guice</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependencies>
The actual integrations happens in the init
method, which calls the addComponentInstantiationListener
method.
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.guice.GuiceComponentInjector;
public class NavigatorApplication extends WebApplication {
@Override
public Class<? extends Page> getHomePage() {
return Startpage.class;
}
@Override
protected void init() {
super.init();
Injector injector = Guice.createInjector(new WebAppModule());
addComponentInstantiationListener(
new GuiceComponentInjector(this, injector));
}
}
I have successfully implemented a solution where wicket's configuration and startup are written purely in java code using Guice's ServletModule - no xml used for wicket at all.
All the details are described here in a blog post I've written.
Full source (zip/svn) and a working example eclipse project are available for download as well (links are at the end of the post).
I think you'll find it great to once again forget about web.xml maintenance :)
精彩评论