Spring extension - A complete example

Introduction

This example is a Spring-enabled but otherwise functionally equivalent
version of the bookmarks
example from chapter 7 of RESTful Web
Services by Richardson and Ruby. The complete code for this version is available
through CVS from :pserver:anonymous@cvs.cs.luc.edu:/root/laufer/433,
module BookmarksRestletSpring. Project dependencies are managed using
Apache
Maven,
and the example illustrates standalone and servlet-container deployment.

In a nutshell, Spring handles the configuration of the top-level Restlet
Component and Router beans. The Restlet Resources had to be modified to
support the init method and the injection of the dependency on the
db4o ObjectContainer, which is also configured in Spring. As expected, the
domain objects User and Bookmark remained unchanged.

Description

First, we show the configuration of the Restlet Component and top-level
Router beans. The top-level Router is necessary only if an non-root
context path is required for standalone deployment.

As a result, the main method has become very simple. It loads a Spring
context based on two configuration metadata files, one for the preceding
top-level beans, and one for the application-specific beans shown below.
It then starts up the top-level Restlet Component.

Next, we look at the configuration of the application-specific Router.
We use a SpringRouter for this purpose, which is configured using a map
of URI patterns to resources. The SpringFinder beans provide the extra
level of indirection required to create Resource instances lazily on a
per-request basis.

In this example, the last URI pattern has to be customized to accept
complete URIs (possibly including slashes) as the last component of the
pattern. We use Spring's nested properties to drill into the
configuration of the URI pattern along with Spring's mechanism for
accessing a static field in a class.

Unlike the preceding singleton beans, we define the ServerResources as
prototype beans so that they get instantiated separately for each
request. All of the Resource beans depend on the
db4o ObjectContainer and are configured analogously, so we show only
UserResource here.