Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Spring or Guice for a Tomcat/Wicket/Hibernate project?

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.

like image 932
Trevor Allred Avatar asked Sep 23 '09 02:09

Trevor Allred


2 Answers

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.

like image 192
Steve Avatar answered Oct 21 '22 13:10

Steve


If you do end up going with Guice, definitely check out Warp Persist for Hibernate, Guice Servlet for Tomcat, and wicket-guice for Wicket.

like image 39
Jesse Wilson Avatar answered Oct 21 '22 14:10

Jesse Wilson