Spring into Seam, Part 1: Build a Spring-Seam hybrid component

Which is better, Seam or Spring? Learn why you don't have to choose

Cover image
 

An excerpt from Seam in Action

By Dan Allen

Published by Manning Publications

ISBN: 1933988401

JBoss Seam is an intriguing framework that unifies JSF, EJB 3, JPA, and JAAS under an accessible component-based model. But adopting Seam doesn't mean that you have to lose the advantages of other frameworks, such as Spring. In this series, excerpted from Seam in Action (forthcoming from Manning Publications), author Dan Allen shows you how -- and why -- Seam and Spring can work together. In this first part, learn how to build Seam-Spring hybrid components that benefit from functionality provided by both Seam and Spring containers.

If you look inside the golf bag of any non-sponsored golfer, you are likely to find a wide variety of equipment brands. A golfer may feel that a Nike putter provides the best touch around the green, that a TaylorMade driver helps stay out of the woods, and that Cleveland irons are the most forgiving of a bad swing. Perhaps some of it is mental. But the true reason behind this diversity is the fact that equipment companies specialize in different products. One company may not have the same passion for designing great putters that they have for designing big shot drivers. For a golfer, one brand doesn't always suffice. The same can be said of a developer's toolbox.

To foster such diversity, JBoss Seam provides a module for integrating the Spring Framework into your Seam application -- and trust me, this is more than just an academic exercise. Seam unifies JavaServer Faces, Enterprise JavaBeans 3, the Java Persistence API, and the Java Authentication and Authorization Service under an accessible component-based model, empowering you to create stateful enterprise applications with tremendous agility. Still, Spring sports many features that you won't find in Seam. It's likely that these features -- such as lightweight remoting, advanced aspect-oriented programming (AOP) declarations, framework template classes, and emulated container resource injections, to mention a few -- simply aren't the passion of Seam. This disparity is not necessarily a shortcoming. You don't have to throw out Seam, along with its stateful architecture and many enterprise integrations, just because it is missing a feature that Spring boasts. Instead, you should recognize that these differences translate into more tools for your toolbox, assuming that you decide to intermix the two frameworks. For that purpose, Seam fully sanctions this integration and encourages the two containers to share both components and low-level resources.

In this three-part series (an excerpt from my upcoming book, Seam in Action), you'll learn how to leverage the Spring container from Seam and vice-versa. This series of articles is designed for developers who are using Seam or JSF and would like to be able to take advantage of capabilities unique to Spring, or those who are contemplating a switch to Seam as a primary platform but don't want to give up their investment in Spring. The text is phrased with the assumption that you understand the basic concepts of Seam and Spring, with limited references to JSF. However, even without this background, you should be able to appreciate the aim of the Spring-Seam integration from a technology evaluation standpoint. The motivation for writing this series is to let community know that Seam and Spring are not mutually exclusive technologies and that it is quite possible their combination offers more value than either one standing alone.

In this first article in the series, you'll be introduced to the Spring-Seam hybrid component, which is a managed object that benefits from functionality provided by both the Seam and Spring containers. In Part 2, you'll learn how to infuse state into traditionally stateless Spring beans by allowing them to reside in Seam contexts, and how to inject stateful Seam components into Spring beans. Finally, in Part 3, you'll learn how to integrate Seam and Spring at the most basic level by having them share a persistence manager.

By the end of the series, you will walk away as an enlightened developer, no longer interested in the trite Spring versus Seam debates, but rather looking for more ways to extract value out of the unmatched features of both frameworks. To you, it's all gravy! Let's start by learning how to get the two containers to play nice with one another.

Seam and Spring, working together

One way to integrate Spring beans into a Seam application is by having the Seam container communicate with Spring indirectly through the use of a custom EL (expression language) resolver. This approach is commonly used for integrating Spring with JSF, which in turn gives Seam access to Spring beans. When the EL resolver processes an expression (e.g., #{tournamentManager.tournaments}), it attempts to resolve the base object by scanning both the Spring and Seam containers for a component with a name that matches the expression root (e.g., tournamentManager), falling back to the JSF-managed bean container if the search comes up empty. While this technique may suffice for simple use cases, there is a lot to gain by establishing a genuine integration between the two containers. The goal is to have the components recognize the presence of one another, rather than having the EL resolver mediate their interaction.

In order to allow cross-breeding to occur, it is essential to have the two containers start up simultaneously rather than in isolation. Otherwise, you could face an impossible situation where each container has a dependency on a startup component from the other, preventing either container from being able to load first. Fortunately, Seam can facilitate a tandem startup, taking care of resolving any circular prerequisites.

1 2 3 4 5 6 Page
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more