Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Building Spring-Seam Hybrid Components For Web Applications

Building Spring-Seam Hybrid Components For Web Applications

This item in japanese

Spring and JBoss Seam frameworks provide different set of features for developing enterprise web applications. Spring is a JavaEE middleware framework, built on features like POJO Abstraction, Dependency Injection(DI) and Aspect Oriented Programming (AOP), which provides integration with frameworks like Hibernate, TopLink and iBatis. Seam is a web application framework that integrates technologies such as AJAX, Java Server Faces (JSF), Enterprise Java Beans (EJB3), Java Portlets and Business Process Management (BPM). Is it possible and does it make sense to use these two frameworks together in developing web applications? This topic was the main focus of a recent article and a java community forum discussion with emphasis on how the strengths of each of these frameworks can be used together to get best of both worlds.

Dan Allen recently wrote two articles about integrating Spring and Seam frameworks in web applications. In the first article, he explained how to build hybrid components that benefit from both Seam and Spring container functionality. He said that Seam unifies JSF, JPA, EJB 3 and other technologies but it doesn't support some of the features Spring offers out of the box, such as lightweight remoting, AOP declarations, framework template classes and resource injections. This is where building a Spring-Seam hybrid component, a managed object that benefits from functionality provided by both the Seam and Spring containers, comes into picture. Seam provides a module for integrating Spring Framework into the web application as well as share application components and low-level resources.

Using a sample web application, the author explained how to integrate Spring beans into a Seam application by using the ContextLoader component provided by Seam and by nesting a seam:component inside the Spring element to decorate a Spring bean as a Seam component. He also said that another way to integrate Spring and Seam is to have the Seam container communicate with Spring indirectly through the use of a custom EL (expression language) resolver.

In part 2 of the article series, he talked about adding stateful behavior to Spring beans, which is still a challenge to accomplish when using Spring framework alone. Spring beans can be infused with "State" by registering a Seam custom scope handler that allows Spring beans to be stored in Seam contexts to make the temporary conversation (for implementing the redirect-after-post pattern), long-running conversation (for single-user page flows), and business process scopes to support interactions from multiple users over an extended period of time. This way, the Spring beans can be stateful without having to resort to the HTTP session.

The author discussed the scope issues and thread-safety problems at run-time when using Spring and Seam together. Spring injects dependencies once at creation time, whereas Seam performs injection dynamically, before every method invocation. This difference in the design philosophy of these two frameworks can lead to "scope impedance" paradox where a bean outlives its intended lifetime as a result of attaching itself to a longer-lived component. This situation occurs when using Spring's static dependency injection to wire components with different scopes. Seam offers a solution to help avoid both scope impedance and thread-safety problems. Instead of injecting a bean instance directly using a "ref" element, you inject a proxy using the seam:instance tag.

There has also been interesting discussion on this topic in the community forums. Rick Hightower gave an overview of Seam 2.0.1 features and led a discussion thread on using Seam and Spring together. Some of the questions that have been asked in the discussion are:

  • If you are using Seam and Spring, and you had to live without one of them, which one would it be?
  • Do you use Seam with or without EJB3?
  • Do you use Seam with or without Spring?
  • How good are the Seam generation tools?
  • Is Seam the best way to write a JPA and JSF based application?
  • Where do you think Spring and Seam overlap and when they do overlap which one is better?

Spring version 2.5.4 and Seam 2.0.1 GA were released recently. For more InfoQ news on these two frameworks, see Spring and Seam sections on the website.


Rate this Article