Spring into Seam, Part 2: When stateless and stateful collide

Add stateful behavior to your Spring beans

Cover image

An excerpt from Seam in Action

By Dan Allen

Published by Manning Publications

ISBN: 1933988401

If you're using multiple development frameworks in a single application, you'll want them to complement each other. For instance, it's hard to add stateful behavior to Spring beans -- but not when you put JBoss Seam to the task.

Too many developers think of a development framework choice as some kind of irrevocable decision. If you choose a framework for its strengths, this thinking goes, you're stuck with its weaknesses, too. But in many cases, you can use multiple frameworks in the same applications to allow their strengths to complement each other.

In the first article in this series ("Spring into Seam, Part 1: Build a Seam-Spring hybrid component"), you saw some of the details of how two popular Java development frameworks -- Spring and JBoss Seam -- can work together. You learned how to build a Spring-Seam hybrid component that serves as a citizen of both frameworks. In this article, you'll see how these components can bring the strengths of one framework to the other. Specifically, you'll see how Seam can add stateful behavior to Spring beans -- something that is still a challenge to accomplish with Spring alone.

Spring, meet state

If there's one thing you noticed in the first article in this series, it's that creating Spring-Seam hybrid components meant typing <seam:component> an awful lot. That's kind of a downer; after all, the goal of Seam is to reduce typing, and this tag is just adding to the pile of XML mess in Spring. Fortunately, there is an easier way to achieve the same end that leverages another highly anticipated feature of Spring 2.0: custom scopes. Custom scopes expand the choices for where instances of Spring beans can be stored.

While Spring added the mechanism to support custom scopes, the built-in implementations are stuck in the past, only covering the scopes in the Servlet API (request, session, and application), plus a stateless scope named prototype. These scopes are ill-suited for modern business applications. To bring Spring up with the times, Seam registers a custom scope handler that allows Spring beans to be stored in Seam contexts. Uses include the (temporary) conversation scope for implementing the redirect-after-post pattern (sometimes called a flash); the (long-running) conversation scope for single-user page flows; and the business process scope to support interactions from multiple users over an extended period of time. At last, Spring beans can be stateful without having to resort to the crutch of the HTTP session!

As it turns out, specifying a Seam scope on a Spring bean has the same effect as applying a nested <seam:component> tag to the bean definition in that it results in a Spring-Seam hybrid component. A two-for-one deal! To take advantage of this feature, you first need to register the Seam scope handler in any one of the Spring configuration files (but not more than once) using the tag in Listing 1. That configuration file must also declare the Seam namespace on the root element, which was explained in the first article in this series.

Listing 1. Registering a Seam scope handler in Spring


To assign a Seam scope to a Spring bean, thus creating a Spring-Seam hybrid component, you simply set the scope attribute on a Spring <bean> definition -- or any custom namespace element in the Spring configuration that supports the scope attribute -- to one of the Seam scopes. The name of the scope is not case-sensitive.

1 2 3 4 5 Page 1
Page 1 of 5