Should I use Spring or Guice for a Tomcat/Wicket/Hibernate project?
If you like the "do-it-all-in-Java" philosophy that Wicket follows, then you might prefer Guice over Spring. There is no XML configuration in Guice - it is all done using the Guice Module
class.
For example, your Wicket WebApplication
class might look something like this:
public class SampleApplication extends WebApplication{ @Override protected void init() { addComponentInstantiationListener( new GuiceComponentInjector(this, new GuiceModule())); }}
The GuiceComponentInjector
comes from the wicket-guice extension. Here's the Module:
public class GuiceModule extends AbstractModule{ @Override protected void configure() { // Business object bindings go here. bind(Greetings.class).to(GreetingRepository.class); }}
In this example, Greetings
is an interface implemented by a concrete GreetingRepository
class. When Guice needs to inject a Greetings
object, it will satisfy the dependency with a GreetingRepository
.
I have put together a sample project that demonstrates how to build a Wicket/Guice application for Google App Engine. You can safely ignore the App Engine specifics and focus on how the Wicket-Guice integration works.
If you do end up going with Guice, definitely check out Warp Persist for Hibernate, Guice Servlet for Tomcat, and wicket-guice for Wicket.
Spring would probably give you more flexibility, but if you just need DI then Guice may be a better choice.
It is difficult to answer as Spring has so many features that would make the DAO more flexible, and works well with Hibernate. It would help if you had more requirements for what you are looking for.
Here are a couple of comparisons between Spring and Guice and Spring, Guice and Picocontainer.
http://code.google.com/p/google-guice/wiki/SpringComparison
http://www.christianschenk.org/blog/comparison-between-guice-picocontainer-and-spring/