RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Try Declarative Programming with Annotations and Aspects

Learn how to combine the power of annotations with aspects to provision enterprise services declaratively, in an EJB 3.0-compatible manner, while still providing container independence.

n our shared quest for more highly productive methods of developing software, those of us in the Java community have looked to J2EE to provide solutions for the more challenging technical problems of enterprise development such as distributed transaction management, concurrency, and object distribution. The idea behind this—that these complicated enterprise services could be implemented by application server vendors and leveraged by business developers—is indeed a very good idea. J2EE, and EJB specifically, has successfully provided a platform on which to build enterprise Java applications.

Part of this success results from the enablement of declarative programming, a style of development in which infrastructure services are declared rather than being explicitly coded and interspersed with business logic. EJB has proven the value of this style of programming by allowing enterprise concerns, such as transactions and security, to be declared in a deployment descriptor and handled by the container.

Within the last couple of years, though, more and more developers have come to recognize that EJB brings its own set of challenges to a team's productivity. Each EJB must be accompanied by multiple interfaces, described in a deployment descriptor, accessed via JNDI, etc. Performing unit tests on EJBs outside of a container presents additional difficulties, and EJBs shift the emphasis away from purer forms of object-oriented development. For more information on the debate surrounding EJB consult the related resources sidebar (see left column).

What You Need

EJB 3.0 aims to ease enterprise development in the following ways:

  • Introduction of metadata annotations to declaratively request enterprise services
  • Dependency/resource injection via annotations
  • Decoupling enterprise beans from EJB-specific interfaces
  • Simplified persistence via light-weight object-relational mapping

This comes as a breath of fresh air to EJB developers who have wrestled with developing, testing, and maintaining EJBs. With EJB 3.0 writing an enterprise bean will now be as simple as writing a POJO (Plain Old Java Object) with special annotations to identify it as an EJB and to request enterprise services. An example of an EJB from the EJB 3.0 Public Draft is shown below:

public class CartBean implements ShoppingCart 
    private float total;
    private Vector productCodes;
    public int someShoppingMethod(){...};
The EJB 3.0 specification essentially recognizes that what developers need is not a heavy-weight, one-size-fits-all solution, but rather a light-weight, easy-to-use solution that provides a range of enterprise services to the developer. One of the most important ways that EJB 3.0 meets this need is through the decoupling of enterprise beans from the EJB API. And this solution has the interesting ramification that EJBs can now not only be run across different EJB containers, but in fact they can be run within any application framework that recognizes the EJB 3.0 (JSR 220) and common annotations (JSR 250) used to declare enterprise services.

This article does not provide an in-depth exploration of declarative programming, EJBs, aspects, or annotations. Instead it takes a looks at the inter-relatedness of these technologies and how to combine them in novel ways to simplify application development.

In this article you will see how to write an EJB 3.0 compatible bean and provision it with declarative transaction management, security, and resource injection by writing a few simple aspects. I hope that you will gain several benefits from this exercise:

  • Learn three practical uses of aspects (dependency injection, security, and transaction).
  • Become familiar with EJB 3.0 and the ideas behind it.
  • Recognize how the decoupling of EJB from a specific API allows EJB 3.0 compliant services to be provided by light-weight implementations, and not just to EJBs.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date