Test Driven Development

Aspect Oriented Programming

Annotation Based Programming

Inversion Of Control - Part 1

Inversion Of Control - Part 2

Hibernate Interview Questions - Part 1

Hibernate Interview Questions - Part 2

Hibernate Interview Questions - Part 3

Spring Interview Questions

EJB 3.0 Interview Questions

Annotation Based Programming Interview Questions

What is attribute or annotation oriented programming ?

Before looking at attribute oriented programming let's look at code generation processes. There are two kinds of code generation processes.

Passive code generation:

is template driven. Input wizards are used in modern IDEs like eclipse, WebSphere Studio Application Developer (WSAD) etc where parameters are supplied and the code generator carries out the process of parameter substitution and source code generation. For example: in WSAD or eclipse you can create a new class by supplying the "New Java class" wizard appropriate input parameters like class name, package name, modifiers, superclass name, interface name etc to generate the source code. Another example would be Velocity template engine, which is a powerful Java based generation tool from the Apache Software Foundation.

Active code generation:

Unlike passive code generators the active code generators can inject code directly into the application as and when required. Attribute/Annotation oriented programming languages leverages active code generation with the use of declarative tags embedded within the application source code to generate any other kind of source code, configuration files, deployment descriptors etc. These declarative metadata tags are called attributes or annotations. The purpose of these attributes is to extend the functionality of the base language like Java, with the help of custom attributes provided by other providers like Hibernate framework, Spring framework, XDoclet etc. The attributes or annotations are specified with the symbol "@<label>". J2SE 5.0 has a built-in runtime support for attributes.

Let's look at an example. Say we have a container managed entity bean called Account. Using attribute oriented programming we can generate the deployment descriptor file ejb-jar.xml by embedding some attributes within the bean implementation code.

* @ejb.bean
* name="Account"
* jndi-name ="ejb/Account"
public abstract class AccountBean implements EntityBean {

The above-embedded attributes can generate the ejb-jar.xml as shown below using XDoclet (use an Ant script):


What are the pros and cons of annotations over XML based deployment descriptors ?

Service related attributes in your application can be configured through a XML based deployment descriptor files or annotations. XML based deployment descriptor files are processed separately from the code, often at runtime, while annotations are compiled with your source code and checked by the compiler.


  • More verbose because has to duplicate a lot of information like class names and method names from your code.
  • Less robust due to duplication of information which introduces multiple points for failure. If you misspell a method name then the application will fail.
  • More flexible since processed separately from the code. Since it is not hard-coded can be changed later. Your deployment team has a greater flexibility to inspect and modify the configuration.
  • XML files can express complex relationships and hierarchical structures at the expense of being verbose.


  • Less verbose since class names and method names are part of yourcode.
  • More robust because annotations are processed with your code and checked by the compiler for any discrepancies and inaccuracies.
  • Less flexible since annotations are embedded in Java comment style within your code. For example, to define a stateless session EJB 3.0 with annotations, which can serve both local and remote clients:

    @Local ({LocalCounter.class})
    @Remote ({RemoteCounter.class})
    public class CounterBean implements LocalCounter, RemoteCounter {

  • Annotations can hold only a small amount of configuration information and most of the plumbing has to be done in the framework.

    Which one to use ?

    Annotations are suitable for most application needs. XML files are more complex and can be used to address more advanced issues. XML files can be used to override default annotation values. Annotations cannot be used if you do not have access to source-code. The decision to go with annotation or XML depends upon the architecture behind the framework. For example Spring is primarily based on XML and EJB 3.0 is primarily based on annotations, but both support annotations and XML to some degree. EJB 3.0 uses XML configuration files as an optional overriding mechanism and Spring uses annotations to configure some Spring services.

    What is XDoclet ?

    XDoclet is an open source code generation engine for attribute oriented programming from ( So you add attributes (i.e. metadata) in JavaDoc style tags (@ejb.bean) and XDoclet will parse your source files and JavaDoc style attributes provided in the Java comment with @ symbol to generate required artifacts like XML based deployment descriptors, EJB interfaces etc. XDoclet can generate all the artifacts of an EJB component, such as remote & local interfaces as well as deployment descriptors. You place the required attributes on the relevant classes and methods that you want to process.

    Inversion Of Control Interview Questions - Part 1>>>

    Home Clouds