October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Introducing JBoss(r) Seam

  • December 7, 2007
  • By Jim Farley
  • Send Email »
  • More Articles »

Another simplification provided by Seam is the ability to use code annotations to directly bind beans to JSF component names, rather than writing managed-bean entries in the faces-config.xml file. The Seam component model includes annotations that can be used to link an instance of a bean directly to a JSF managed bean name. When the name is used in a JSF (one of its properties is used as the value of an HTML input field, for example), the bean instance will automatically be initialized, if necessary, and used as the backing bean for the JSF. There's no need to connect the bean to the JSF managed bean name using faces-config.xml.

The Seam component model also supports a more general version of dependency injection, called bijection. Standard dependency injection involves a one-time initialization of a bean reference within a component, typically done by some kind of container or other runtime service. Seam bijection extends this to support the following:

  • Two-way propagation of references: A component can have a reference injected by the container, and a component can also "outject" a reference to the enclosing context as well.
  • Dynamic updates: Instead of doing one-time injection of references, bijection is done on each invocation of the component. This is key in the Seam component model, since components can be stateful, and therefore they and their dependent beans can evolve across invocations.
  • Multiple contexts: Dependencies (incoming and outgoing) can be established across multiple Seam contexts, rather than being forced to exist within a single context. So a session-scoped component can inject request-scoped beans and outject application-scoped beans, for example.

This may all sound a bit esoteric at this point, but the value of these features in the Seam component model will be clear once I show you some example code.

Running Example: A Gadget Catalog

The example we're going to use is an online catalog of high-tech gadgets (mobile phones, laptops, digital media players, etc.). For now, we'll start with a very simple application that can only do two things:

  • Display a list of gadgets contained in the catalog.
  • Allow the user to enter a new gadget into the catalog.

At this point, our model for the application will be painfully simple: a gadget will only consist of a description (e.g., "Acme Powertop X1 Laptop") and a type (e.g., "laptop"). The data about these gadgets will be stored and managed in a relational database. The page-flow for the user interface will be equally simplistic: a main page will display the list of gadgets in the database and offer a single option to add a new gadget to the database. This option will bring the user to an entry form that prompts for the necessary attributes, and on submission the new gadget will be stored in the database, and the updated list of gadgets will be displayed again.

We can represent the "solution design" at this point with a pageflow diagram and a relational database diagram. The pageflow for the first iteration of the Gadget Catalog is shown in Figure 3, and the database structure (such as it is) is shown in Figure 4.

Figure 3. Gadget Catalog pageflow

Figure 4. Gadget Catalog database

Now all we have to do is build it. As a point of reference, let's first see what the Gadget Catalog looks like in the vanilla Java EE framework.

The Gadget Catalog Without Seam

The code for this example can be found here. In the Java EE framework, without JBoss Seam, the customary approach to implement the Gadget Catalog is to use JSF for the UI in conjunction with EJBs for the backing business logic and persistence.

To start, we'll implement an EJB 3.0 entity bean to represent the gadgets to be stored in the GADGET table. Listing 1 shows the Gadget bean. This is a simple EJB 3.0 entity bean that is mapped to the GADGET table using the EJB @Table annotation. The bean has two persistent properties: the description property is mapped to the DESCR column, and the type property is mapped to the TYPE column.

Listing 1. Gadget Entity EJB

@Entity
@Table (name= "GADGET")
public class GadgetBean implements Serializable {
   private String mDescription = "";
   private String mType = "";

   public GadgetBean() { }

   @Id
   @Column (name="DESCR")
   public String getDescription() {
      return mDescription;
   }

   public void setDescription(String desc) {
      mDescription = desc;
   }

   @Id
   @Column (name="TYPE")
   public String getType() {
      return mType;
   }

   public void setType(String t) {
      mType = t;
)




Page 2 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel