YOUR FEEDBACK
The Cloud Wars - Is Guitar Hero a Cloud?
Roland Judas wrote: I am following the cloud discussions for some months n...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Annotations, Friend or Foe?
It has quickly become one of the most popular, and yet most controversial, language feature in core Java

Digg This!

Annotation is a new Java language feature introduced in JDK 5.0. It has quickly become one of the most popular, and yet most controversial, language feature in core Java. New Java frameworks, such as EJB 3.0 and Hibernate 3.0, make extensive use of annotations to eliminate the excessive XML configuration files (a.k.a. the "XML hell" in Java EE). Those annotations significantly reduce the amount of code and configuration data, and simplify the overall architecture, making application development easier. At the same time, enterprise architecture purists are complaining that annotations corrupt the separation between code and configuration, reduce the overall level of abstraction, and create more coupling between code and external frameworks. They argue that annotations should not be used to specify relationships between application components. Who is right? My view is that both arguments have merits. But as a developer, you really should make decisions based on the project at hand.

To understand which projects would benefit from annotation frameworks, we have to know a little bit of history on how annotations were introduced and evolved in the Java language. From the beginning, Java was a pure pro-gramming language to specify the behavior of the system. However, the whole idea of Java EE is to use "containers" to deliver reusable services (e.g., transaction, persistence, security, network connection, etc.) to Java objects. You have to somehow specify the relationships and dependencies between Java objects and container services. One approach is to use API calls directly in your Java components to consume container services. Java servlets and JNDI clients are such examples. However, too many framework APIs inside a Java class make the code very difficult to read and maintain (i.e., lots of boilerplate code). So, XML configuration files are widely used to specify the relationship between objects and services. This declarative approach allows you to configure the objects from the outside instead of cluttering up the business logic code with framework API calls. In fact, a large part of the Java EE specification is to standardize those XML files. Newer frameworks such as Spring even adopt an approach to put all configuration information in XML files, so that the Java business objects have zero dependency on the external frameworks (Dependency Injection via XML files). That is, you can apply services from any container to the same Java objects by just changing the XML files. This level of abstraction is great from an architecture point of view. However, it also creates huge complexity for developers and results in something known as the "XML hell."

Using XML to configure Java code has several distinct disadvantages: since XML files are processed separately from the code, they have to duplicate information already contained in the code (e.g., class names and method names, the inheritance structure, etc.) in order to configure the code. The duplication makes XML verbose even for the simplest configuration. Such XML files are also error-prone since spelling errors are not checked by the compiler or even the deployer. At the end of the day, XML configuration files simply shift the maintenance complexity and boilerplate code from Java classes to XML files. Developers are searching for better solutions.

A popular solution that emerged from the open source community is XDoclet. It uses embedded tags in Javadoc comments to specify which container services should be applied to this class or method. Then, a Javadoc-like preprocessor takes in the source code and generates all the necessary XML files based on the relationship. Since the XML configuration files are automatically generated, it's guaranteed to be correct and consistent. The developer only needs to maintain the XDoclet tags that are inside the source code file but don't clutter up the code like boilerplate code used to. The popularity of XDoclet is telling. It indicates that a large number of developers would prefer ease-of-development over added abstraction.

To capitalize on the success of XDoclet, Sun decided to introduce annotations as a formal language feature in Java. Instead of being a second class citizen embedded in code comments, annotations are now fully supported by the compiler, IDE tools, and are standardized into APIs. Using annotations, you can specify how container services are delivered to Java objects. For instance, the following example is an EJB 3.0 entity bean. The @Entity annotation tells the container that this class should be mapped to a database table. The @Id(generate=AUTO) annotation tells the container to automatically generate the primary ID value when the object is saved into the database.

@Entity
public class Reply implements Serializable {

private long id;
private String name;

@Id(generate=AUTO)
public long getId() {
   return id;
}

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

To use the entity bean, the @PersistenceContext annotation tells the container to inject a valid EntityManager object to the Manager-Action object. The @Transaction-Attribute annotation declares that the reserve() method is managed by the container's transaction manager, which commits changes to the database at the end of the current thread.

@Stateless
public class ManagerAction implements Manager {
private Reply reply;

@PersistenceContext
private EntityManager em;

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void reserve () {
em.persist (reply);
}
}

In addition, you can also use annotations to manage relationships between your own objects. The following example uses annotations in the JBoss SEAM Web framework. The @In annotation below injects the reply object from the Web context, which is probably mapped to fields in a JSF form requesting a user reply. This way, when the user changes the reply object from a JSF form, it is automatically validated and propagated to the ManagerAction object. Likewise, the @Out annotation outjects the allReplies variable updated in the reserve() method to the context so that all the replies can be displayed in a table on a JSF Web page.

public class ManagerAction implements Manager {

@In
private Reply reply;

@PersistenceContext
private EntityManager em;

@Out
private List <Reply> allReplies;

public String reserve () {
em.persist (reply);
allReplies = em.createQuery("from Reply r").getResultList();
return null;
}
}

Those annotations eliminate boilerplate code in both Java and XML code. The annotated code is very easy to understand. However, annotations also have a few downsides. Annotation is not as expressive as XML and it's only weakly validated at compile time. For instance, you could mistakenly place the @PersistenceContext annotation on the reply variable and the compiler would not complain. The application only fails when it deploys. In addition, perhaps the biggest argument against annotations is that they mix configuration with the Java code, and hence tie your Java objects to the particular service framework that defines the annotations (EJB 3.0 or JBoss SEAM in our examples). If you want to switch to another framework later, you'd have to go into the Java class code and change the annotations. In contrast, XML-configured Java objects (e.g., Spring POJOs) can work with any service framework without changes to the Java class - you only need to change the XML files - due to the clean separation between code and configuration. Of course, the XML approach only has an advantage when you have far more Java code than XML code, which is not the case for most business applications today.

What is the verdict? If your objects have very complex relationships or if "framework independent" business logic is essential to you, you could be better off choosing an XML-based configuration framework such as Spring or JBoss Micro-container. However, if you deal with medium-complex systems and are concerned about productivity over abstraction, annotations are perfect. The standardization of annotations in specifications like EJB 3.0 will eventually make many annotations part of the Java core API. Framework independence is less of an issue in a world with standardized frameworks. We will use annotations just as we make API calls to JDK libraries today. That is, of course, the intention Sun had when it introduced annotations into the Java language.

About 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/.

jdj news desk wrote: Annotation is a new Java language feature introduced in JDK 5.0. It has quickly become one of the most popular, and yet most controversial, language feature in core Java. New Java frameworks, such as EJB 3.0 and Hibernate 3.0, make extensive use of annotations to eliminate the excessive XML configuration files (a.k.a. the 'XML hell' in Java EE). Those annotations significantly reduce the amount of code and configuration data, and simplify the overall architecture, making application development easier. At the same time, enterprise architecture purists are complaining that annotations corrupt the separation between code and configuration, reduce the overall level of abstraction, and create more coupling between code and external frameworks.
read & respond »
JDJ News Desk wrote: Annotation is a new Java language feature introduced in JDK 5.0. It has quickly become one of the most popular, and yet most controversial, language feature in core Java. New Java frameworks, such as EJB 3.0 and Hibernate 3.0, make extensive use of annotations to eliminate the excessive XML configuration files (a.k.a. the 'XML hell' in Java EE). Those annotations significantly reduce the amount of code and configuration data, and simplify the overall architecture, making application development easier. At the same time, enterprise architecture purists are complaining that annotations corrupt the separation between code and configuration, reduce the overall level of abstraction, and create more coupling between code and external frameworks.
read & respond »
LATEST JAVA STORIES & POSTS
Saving Your Investment: Transforming J2EE applications into Web 2.0 using GWT
The pressure is on to keep pace with Web 2.0 entrants into the marketplace. Rewriting is expensive; adding AJAX widgets results in a complex, unmaintainable application. Both require you to hire scarce JavaScript developers. Google Web Toolkit -- the SDK that allows you to write
WSRP Really Works! - Part 2
A standard from OASIS called Web Services for Remote Portlets (WSRP) is used so portlets can be decoupled from a portal. In part one (JDJ, Volume. 13, issue 3) of this article, we introduced the relevant standards and specifications and then demonstrated WSRP's capabilities by co
Adobe's Kevin Lynch and Microsoft's Scott Guthrie to Keynote AJAX World RIA Conference & Expo
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted
Sun Expects Q4 Earnings Above Estimates
On Tuesday evening Sun issued a fourth-quarter guidance range largely above analysts' estimates. The company pre-announced that revenue for its fiscal fourth quarter ended June was $3.725 billion to $3.8 billion, with gross margin in the 44-45% range. Sun expects non-GAAP profits
Virtualization Conference Keynote Webcast Live on SYS-CON.TV
Brian Stevens, the Chief Technology Officer and Vice President of Engineering of Red Hat, delivered his Virtualization Keynote 'The Future of the Virtual Enterprise' at SYS-CON's Virtualization Conference & Expo 2007 West in San Francisco. 'Virtualization is the hottest subject
The Beauty of JavaScript
JavaScript is one of the most interesting and misunderstood programming languages in common use today. Most developers will go their entire careers without realizing its full potential. It's not often that you get a language that supports the feature set that JavaScript does, whi
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
SOA in a JVM: OSGi Service Platform - A Dynamic Component System for Java
There are many forces that influence technological evolution. After a decade of building enterprise
AJAX and Enterprise RIA Tools - JSF, Flex, and JavaFX
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver
Final Voting Phase on OpenAjax Browser Wishlist
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated
AJAX World RIA Conference News - Netflix UI Guru To Present on Crafting Rich Web Interfaces
In every field of design one of the first things students do is learn from the work of others. They
Infragistics Releases CTP UI Components for Microsoft Silverlight Beta 2
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI
Yahoo User Interface 2.5.2 Released
The YUI development team has released version 2.5.2; you can download the new release from SourceFor
ADS BY GOOGLE
BREAKING JAVA NEWS
Domark International, Inc. Completes Its Acquisition of Javaco, Inc.
Domark International, Inc. (OTCBB:DOMK) announced today that it has completed its acqui