Using Google Guice in Web Applications

Introduction As promised in the previous article I’ll continue presenting Google Guice also for web applications. In order to do ... Continue reading

Introduction

As promised in the previous article I’ll continue presenting Google Guice also for web applications. In order to do this you’ll need to get the servlet extension – part of the standard distribution, along with other extensions like JMX, JNDI, Persist, Struts or Spring. Using Guice, the web.xml will be reduced at minimum – just make the Guice container start. The rest of the configurations will be easily done in Java in the same type-safe manner presented in the previous article.

The servlets will benefit from:

Constructor injection

Type-safe configuration

Modularization

AOP

In this article I’ll present the following scenarios:

Developing a web application from scratch

Adding Guice to an existing web application

Starting a new web application using Guice

Check the bellow image for an Eclipse screenshot with the project structure.

Besides the core Guice libraries presented in the previous article, we must also add the guice-servlet.jar in the application’s class path (please check the end of the article for the Maven dependency).

After the class path is properly configured, we must first define the Guice Filter in
web.xml. This will actually be the only configuration present in this file.

Next step is creating the Injector and defining the Modules. Besides the “normal” modules used by an application – presented in the previous article – in order to actually use the Guice Servlet module we must declare an instance of com.google.inject.servlet.ServletModule. This Module is responsible for setting the Request and Sessions scopes and this is the place where we’ll configure the servlets and filters within the application.

Considering that we write a web application, the most logical and intuitive place to create the Injector is within a ServletContextListener. A
ServletContextListener is a component that fires just after the application is deployed and before any request received by the server. Guice comes with its own class that must be extended in order to create a valid injector. As we’ll use Servlet 3.0 API we’ll annotate this class with @WebListener – we won’t need to declare it in
web.xml.

I was saying that the ServletModule is the place where we configure our servlets and filters. This is the content of the class that it will extend this module. It will configure a servlet mapped to all the .html requests:

Call injector.injectMembers(this) –this will inject all the dependencies

Callinjector.getInstance(clazz) for each instance that needs to be injected

Request and Session scope

The servlet extension adds 2 new scopes: Request and Session. We’ll see next an example of using the Session scope.

We’ll slightly modify some of the classes presented before. Considering that we’ll need to mix scopes and we want to access an object with a narrower scope from an object with a wider scope (access a Session scoped object from a Singleton) we’ll use Providers (see the Note section for details).

In order to demo the functionality we’ll access the application twice in the same session. The first time it will display the below result:

The second time it will display the following result:

This example can be easily changed in order to use Request scope instead of Session scope.

This is how a simple Guice web application looks like. I tried to touch the most important points from the Guice Servlet extension. As mentioned in the previous article, this is just a small intro. You can continue experimenting different situations and you’ll learn the most by actually using Guice into a real project.