Question

I'm building a new web application that uses Linux, Apache, Tomcat, Wicket, JPA/Hibernate, and MySQL. My primary need is Dependency Injection, which both Spring and Guice can do well. I think I need transaction support that would come with Spring and JTA but I'm not sure.

The site will probably have about 20 pages and I'm not expect huge traffic.

Should I use Spring or Guice?

Feel free to ask and followup questions and I'll do my best to update this.

Was it helpful?

Solution

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.

OTHER TIPS

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/

Don't forget CDI/JSR-299, part of Java EE 6. You can use weld-wicket to integrate wicket with CDI.

(as long as you're using the weld implementation (as GlassFish v3 and JBoss 6 do), but the weld-wicket is rather small so you could probably adapt it if needed).

I managed to get Wicket 1.4 + weld-wicket + wicket-contrib-javaee + EJB 3.1 + JPA 2.0 + wicket-security (SWARM) + Spring Security 3 + Spring 3 running together in a small proof of concept application. That's a bit too many frameworks though, will probably drop spring-security & spring as they appear redundant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top