home

Guice with a Spring Twist

23 Aug 2009

My second venture into Guice Land is an approach at integrating a Spring MVC web tier into a Guice application. My goal was to assume a Guice-only application tier which needed to be exposed via the web. I built on my previous Guice project to remove the limited Servlet and add a Spring MVC layer to achieve the same end.

Recall the service interface, Greeter, and the default implementation, DefaultGreeter.

package com.earldouglas.simpleguice;

public interface Greeter {

  public String getGreeting();
}
package com.earldouglas.simpleguice;

public class DefaultGreeter implements Greeter {

  public String getGreeting() {
    return "Hello World!";
  }
}

Also recall the simple Guice Module, which binds Greeter to DefaultGreeter.

package com.earldouglas.simpleguice;

import com.google.inject.AbstractModule;

public class SimpleGuiceModule extends AbstractModule {
  
  @Override
  protected void configure() {
    bind(Greeter.class).to(DefaultGreeter.class);
  }
}

Instead of the SimpleGuiceServlet, a Spring @Controller is used. This is the point at which the Guice-Spring integration takes place. SimpleController is dependency injected (by Spring, in the web tier) with a Guice Injector, from which the controller can retrieve a Greeter from Guice. This Greeter is then used to put the corresponding greeting on the model.

package com.earldouglas.guicespringmvc.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.earldouglas.simpleguice.Greeter;
import com.google.inject.Injector;

@Controller
public class SimpleController {

  @Autowired
  private Injector injector;

  @RequestMapping(value = "/greeter.html", method = RequestMethod.GET)
  public void greeter(Model model) {
    model.addAttribute("greeting", injector.getInstance(Greeter.class)
        .getGreeting());
  }
}

A simple JSP view is used to display the greeting on the model.

greeter.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:out value="${greeting}" />

The web tier belongs to Spring MVC, whose configuration includes a Guice Injector constructed from SimpleGuiceModule, as well as the support infrastructure to process @Controllers, @RequestMappings, and to use JSPs to render views.

guicespringmvc-servlet.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <bean class="com.google.inject.Guice" factory-method="createInjector">
    <constructor-arg>
      <bean class="com.earldouglas.simpleguice.SimpleGuiceModule" />
    </constructor-arg>
  </bean>

  <bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
  </bean>

  <context:component-scan base-package="com.earldouglas.guicespringmvc.web" />

</beans>

Spring MVC is configured in web.xml in the standard way.

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>
    <servlet-name>guicespringmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>guicespringmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

</web-app>