Guice with a Spring Twist

August 23, 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;


public class SimpleGuiceModule extends AbstractModule {

    protected void configure() {

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;

public class SimpleController {

    private Injector injector;

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

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


<%@ taglib prefix="c" uri=""%>
<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.


<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:context=""

    <bean class="" factory-method="createInjector">
            <bean class="com.earldouglas.simpleguice.SimpleGuiceModule" />

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

        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />

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


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


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="" xmlns:xsi=""