EJB3 – annotations vs xml

Scott’s recent post on EJB3 got me thinking about annotations as a “replacement” for XML.

By now, we all know why shoving everything in XML isn’t the best of ideas.  I think shoving everything in Java code is bad too.  In particular deployment time concerns (like security) shouldn’t require a recompile.

JEE 5 offers the ability to choose whether to use all annotations, all XML or a mix of annotations and a partial deployment descriptor.  Yet most of the articles and books I’ve seen encourage using annotations for everything and the deployment descriptor as some kind of legacy practice or anti-pattern.  This reminds me of the situation where most people agree code in a JSP is bad practice and yet it keeps coming up due the vast quantity of beginner books with code in the JSP.  From reading the books, it sounds like EJB deployment descriptor XML = bad regardless of whether that is the case in practice.

I do think the annotation approach is fine for the mapping – unless you are developing a common component that will be deployed to different schema definitions.  Most of the time, the schema is stable as Scott noted.

Let’s look at some of the things in an EJB deployment descriptor (for a session bean):

  • Bean Type – This is a coding concern and as such fits well in the Java code.  If my bean changes from stateful to stateless, I should be looking at my code.
  • Security Settings -This is a deployment time concern.  There’s no reason a role change should mandate a redeployment.  Or that the developers know this information.  The application assembler or deployer could add this in.  As such, the security settings are well suited to being in an XML file.  This also has the advantage of a reusable component provider being able to provide generic information and the integrating applications specify XML info specific to their application.
  • Transaction Settings – This one could be argued either way.  If certain settings such as “required new” are needed, it makes sense to specify in the code to hint at this.  At the same time, the correct transaction setting could depend on the integrating application.  I think transaction settings could be a use case for specifying as a Java annotation and allowing/suggesting the integrating application override in XML.
  • Resource References -Resources are both a coding concern and a deployment time concern.  The code certainly cares that the correct resources exist.  And the deployer cares that they are linked correctly.  Luckily, this scenario has existed for years and there already exists an approach.  The reference name and link to the JNDI can be specified as Java annotations since they are coding concerns and likely stable.  The deployer has always been responsible for setting up the correct resource in the JNDI.

None of these are hard and fast rules.  They are just meant to get people thinking about when to use Java annotations vs XML for the deployment descriptors.  We don’t want to just use Java annotations blindly because they are there.

As more people migrate to EJB 3, I think we are going to see some of a “I’m not going to have a deployment descriptor at all now that I can do everything in Java” mentality.  We’ll have to see if it is going to take a swing to far in the other direction (no XML) before people realize some things belong in Java while others belong in XML.

2 thoughts on “EJB3 – annotations vs xml

  1. I agree with most of this whole-heartedly.
    However I strongly disagree on Resource References. IMO these belong in a deployment descriptor of some sort. Does the environment even have an LDAP server available? Is one necessary? Could the resource be file based in some deployments and LDAP based in others? What about the resource being class-loader based?

  2. Brett,
    Hmm. Interesting point to consider. What if the resource wasn’t available as in the case of an LDAP server. The code is expecting to read from some resource, right? I would think a file based resource would be available in LDAP under a different name.

    I see two link points on resources. reference <--> JNDI and JNDI <--> actual resource. I suppose one might want to change the reference to point to different JNDI resources in a shared environment. Is this what you mean?

Leave a Reply

Your email address will not be published. Required fields are marked *