Welcome!

Java Authors: Maureen O'Gara, Patrick Curran, Carmen Gonzalez, Michael Sheehan, Jonny Defh

Related Topics: Java

Java: Article

Lightweight Java Enterprise Application Frameworks: JBoss Seam

Lightweight application frameworks are all the rage in the enterprise Java community in the past couple of years

Lightweight application frameworks are all the rage in the enterprise Java community in the past couple of years. From the pioneering Spring and Hibernate frameworks, to the infusion of technologies like aspect-oriented programming and metadata annotation, to the new standard EJB 3.0 (and Java EE 5.0) specifications, lightweight frameworks have gradually become mainstream. The rise of lightweight technologies was largely due to developers' rebellion against the "heavyweight" of EJB 2.1 (and earlier). Lightweight frameworks aim to make developers more productive and the application less error-prone by removing the rigid EJB 2.1 infrastructure classes/interfaces and excessive XML deployment descriptors (commonly known as "XML hell" in EJB 2.1). Beyond that, lightweight frameworks have also promoted better and cleaner application architectures, and make it easier to reuse business components when you switch vendors.

The core principle shared by all lightweight enterprise Java frameworks is the use of plain old Java objects (POJOs) for the data access and business logic. There are no more infrastructure classes or interfaces to inherit or implement. You just create a POJO to model your data or to implement a business process using the data. Then the POJOs are "wired" together using metadata. For instance, you can wire a POJO to a container service that maps the object to an entry in a relational database; you can wire a POJO method to a transactional service to guarantee the atomicy of its database operations or to a security service to limit access to the method; you can also wire POJOs together with each other. A key technique in the wiring of POJOs is a design pattern called Dependency Injection (DI). DI uses the lightweight framework container (e.g., a Spring container or an EJB 3.0 container) to inject services or other objects into a POJO. This way, all object instances are created and managed by the container. There is no need for each POJO to manage the life cycle of its service objects or to look up services. DI eliminates a lot of boilerplate code in applications and makes them easier to understand and maintain.

The major differences between lightweight frameworks are how they wire container services together and implement Dependency Injection. The service architecture and metadata expression are the key issues here. In this issue of JDJ, Chris Richardson, author of Manning's new POJO in Action book, explains how popular lightweight frameworks such as Spring, Hibernate and EJB 3.0 deliver services to POJOs.

While different lightweight frameworks support different metadata syntax for POJO wiring, developers naturally still want to reuse POJOs across frameworks. In his "Spring and EJB 3.0 in Harmony" article, Aleš Justin, an avid Spring and JBoss user, explores how to write a Spring deployer in JBoss. Using the deployer, you can deploy Spring POJOs side-by-side with EJB 3.0 POJOs in the JBoss Application Server. Very cool!

Now, Spring, Hibernate, and EJB 3.0 are lightweight frameworks for the enterprise middle tier. They are great for implementing business and persistence logic. However, to use them in Web applications, you often still need to provide your own integration code with the servlet/Struts/JSF-driven Web tier. Can we use business POJOs directly in the Web tier? In the Spring world, the Spring MVC framework is a relatively new Web framework that works well with Spring business POJOs. What about the standard-based EJB 3.0 world? Is there a framework that allows developers to write complete Web applications using EJB 3.0-style POJOs and annotations? The answer is the JBoss Seam framework. To understand Seam, let's look at a very simple Hello World application: it has a simple Web page where you can enter your name to "say hello" to Seam. When you click submit, your name is added to the database and the page displays all persons who have said hello so far. The application requires only three simple components. First, write an EJB 3.0 entity bean, the Greeter class, to represent a greeter (i.e., the data model). The @Entity annotation tells the container that the Greeter POJO is automatically mapped to a relational database table. The @Name annotation specifies that Seam manages the Greeter instances under the name "greeter". When other application components request an instance of the Greeter bean from Seam, they should use the name "greeter".

@Entity
@Name("greeter")
public class Greeter implements Serializable {

     private long id;
     private String name;

     @Id(generate=AUTO)
     public long getId() { return id;}
     public void setId(long id) { this.id = id; }

     public String getName() { return name; }
     public void setName(String name) { this.name = name; }
}

The Web page looks like the following. Notice that the textfield is mapped to the Seam-managed Greeter entity bean via the name "greeter". When a user enters a value in the text field, Seam automatically creates a Greeter instance encapsulating the user input. When the user submits the form, the event handler method manager.sayHello() is invoked by Seam. This method is defined in an EJB 3.0 session bean POJO under the Seam name "manager", which we will discuss next.

<h:form>
Please enter your name:<br/>
<h:inputText value="#{greeter.name}" size="15"/><br/>
<h:commandButton type="submit" value="Say Hello"
action="#{manager.sayHello}"/>
</h:form>

The event handler session bean, ManagerAction, has a Seam name "manager" as described above, so that it can be referenced in the JSF page. Via the @In annotation, Seam automatically injects the managed Greeter POJO, which has Seam name "greeter', into the ManagerAction property of the same name. The "greeter" object is already populated with the user input from the JSF page. The sayHello() method saves the entity bean POJO to the database and retrieves the current list of greeters, which is out-jected into the Seam context via the @Out annotation.

@Stateless
@Interceptors(SeamInterceptor.class)
@Name("manager")
public class ManagerAction implements Manager {

   @In
   private Greeter greeter;
   @Out
   private List <Greeter> allGreeters;

   @PersistenceContext
   private EntityManager em;

   public String sayHello () {
     em.persist (greeter);
     allGreeters = em.createQuery("from Greeter g").getResultList();
     return null;
   }
}

Since the allGreeters list is out-jected into theSeam context, the JSF page can refer to it and display the name list.

<p>The following persons have said "hello" to JBoss Seam:</p>
<h:dataTable value="#{allGreeters}" var="person">
   <h:column>
     <h:outputText value="#{person.name}"/>
   </h:column>
</h:dataTable>

Now, you can see JBoss Seam is a powerful glue that connects EJB 3.0 POJOs and JSF pages. It also expands the dependency injection pattern into a bi-directional injection. As a result, everything is managed by the Seam context and developers can focus on their core business logic, which is the ultimate goal of any lightweight Java framework. In addition, the Seam context also manages HTTP session state and even workflow in external business process engines (e.g., the jBPM engine). Norman Richards, author of JBoss Developer's Notebook and XDoclet in Action, will tell you more about how Seam makes it super easy to develop stateful Web applications in his article "Seam: The Next Evolution of Web Applications."

By all indications, lightweight technologies are the future of enterprise Java. Learn it, master it, and use it in your best-of-breed new Internet applications!

More Stories By Michael Juntao Yuan

Michael Juntao Yuan is a member of JDJ's editorial board. He is the author of three books. His latest book, "Nokia Smartphone Hacks" from O'Reilly, teaches you how to make the most out of your mobile phone. He is also the author of "Enterprise J2ME" - a best-selling book on mobile enterprise application development. Michael has a PhD from the University of Texas at Austin. He currently works for JBoss Inc. You can visit his Web site and blogs at www.MichaelYuan.com/.

Comments (3) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Tomas Cerny 03/09/08 08:57:34 PM EDT

Hello Michael,

I am sorry for contacting you this way. I tried your JBoss email but it got returned.

please let me start with thanks for your books that helped me with my tool development.

I have developed a new tool for view form generation from entity beans that I think would be very useful for JSF and Seam developers. I would like to get your feedback on the tool and ideas if you have time. I am also trying to get feedback from Seam community (http://www.seamframework.org/Community/FormBuilderSeamgenExtensionAnnoun...).

I already contacted Seam representatives and my announcement was posted to seam-dev group, but I also wanted to contact you personally.

Here are details...

I am a graduate student and ICPC frontend developer working on application that is build on JSF, facelets, Hibernate, Spring and Acegi.
My research is in the next generation web application frameworks for the next generation of our application.

I have developed a tool called FormBuilder that can fully generate view forms from entity beans and offers complete customization of input fields. My tool uses Hibernate-Validation (annotations). Developers can even define their own input components for form generation. When the input components are wrapped as facelet tag, it can offer even more!

Some features:

1. The tool is completely configurable using annotations and XML.
2. Each generated form can function in either read-only or editable model, depending on the "editable" attribute.
3. The form uses configuration-by-exception for its field rendering for security
4. Forms may be referenced by Facelets tags to decrease application coupling.
5. Tool forces good practice for code management (different users same style)
6. Entity field property propagation to the form allows client-side validation
7. I provide two tag libraries with client-side validation and new input component types like Password, Link, Html, ColorPicker. (The first library is Seam+RichFaces, and the second library is simple JSF.)
8. I also created new field annotations like (password, link, html, javaScriptPattern, formOrder) for complete form generation.
9. Form creation and maintenance is completely handled by the tool. After entity bean updates, "fresh" forms are autogenerated to match.

More information is available here:

http://cs.ecs.baylor.edu/~cerny/formBuilder/guide.html

I have also built an example application showing new components and client side validation (tested on 5 main browsers!)
It offers also comparison to forms generated by seam-gen.

You can experiment with the application by going to http://fire.ecs.baylor.edu:8080/FormBuilderExample/home.seam (not official/private)

Currently, you can download FormBuilder from http://cs.ecs.baylor.edu/~cerny/formBuilder/download. Later I am planning to put it on SourceForge.net.

I welcome any feedback (even bad) on this work.

Tomas

SYS-CON Italy News Desk 02/13/06 03:27:15 PM EST

Lightweight application frameworks are all the rage in the enterprise Java community in the past couple of years. From the pioneering Spring and Hibernate frameworks, to the infusion of technologies like aspect-oriented programming and metadata annotation, to the new standard EJB 3.0 (and Java EE 5.0) specifications, lightweight frameworks have gradually become mainstream. The rise of lightweight technologies was largely due to developers' rebellion against the 'heavyweight' of EJB 2.1 (and earlier).

SYS-CON Australia News Desk 02/13/06 03:10:05 PM EST

Lightweight application frameworks are all the rage in the enterprise Java community in the past couple of years. From the pioneering Spring and Hibernate frameworks, to the infusion of technologies like aspect-oriented programming and metadata annotation, to the new standard EJB 3.0 (and Java EE 5.0) specifications, lightweight frameworks have gradually become mainstream. The rise of lightweight technologies was largely due to developers' rebellion against the 'heavyweight' of EJB 2.1 (and earlier).