| By Michael Juntao Yuan | Article Rating: |
|
| May 22, 2006 07:15 AM EDT | Reads: |
54,677 |
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!
Published May 22, 2006 Reads 54,677
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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/.
![]() |
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. 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. 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!) 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). |
||||
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Cross-Platform Mobile Website Development – a Tool Comparison
- Three Buzzwords That Every CIO Hears but One They Should Listen To
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Immersing into JavaScript Frameworks
- Workday Reportedly Prepping to Go Public
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Book Review: Sams Teach Yourself Java in 24 Hours
- OpenOffice.com Lives
- Book Excerpt: Introducing HTML5
- Adobe Sends Flex to the Apache Foundation
- Five Years Waiting for JRE 7: Is It Justified? (Part 1)
- Book Excerpt: Java Application Profiling Tips and Tricks
- i-Technology in 2012: Five Industry Predictions
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- OpenXava 4.3: Rapid Java Web Development
- The Next Web Architecture
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Is Write Once Run Anywhere Ever Going to Be a Reality?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?

















