• Post Reply Bookmark Topic Watch Topic
  • New Topic

advantage of ejb3 :: annotation as against DD  RSS feed

anagha desai
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
I am new to EJB ( only worked for brief time on 1.1 Version )

One of the advantages being touted is use of "annotations" as compared to Deployment descriptor .

In the book "Mastering EJB " - it is stated that XML is prone to errors due to human editing .

1 >
I really find that difficult to digest
Dont we use "web.xml" in servlet apps - dont see issues there ?
struts-config.xml in struts is also xml driven ?

Or are we going to say that these are also problematic ?

2 >
One takeaway from EJB DD was supposedly the point of changing behaviour without changing code ( by changing DD )
With annotation that is lost right ?
So is that an advantage or a disadvantage ?

3 >
Not sure but think have read that DD will override annotations if both are present ?

so does this mean that in dev env - use annotations and in prod use deployment descriptor ?
( if thats true - i would prefer sticking with DD )

Thoughts ideas and criticism are welcome

[ June 09, 2008: Message edited by: anagha desai ]
Aditya Vasudeva
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anagha,

EJB 3 is a lot easier than its previous versions. Its because you've not used it that you dont understand. Here's why(according to how you put it) :

1. Although xml is used in cofiguring servlets and jsp's, we have to remember a lot of stuff in order to write a correct web.xml file. There's a lot of stuff in that file which we could forget easily. On the other hand, annotations are dependency injections. Think of it as an import statement in your code. When you want it just inject it there. You would just have to write a single line of simple java code(which you would be much more comfortable with) rather than writing a seperate xml file for a simple import and that too, a lot of xml for a single thing.
Take an example of Interceptors. Using xml, including an interceptor in your code you would have to write the following :


But, using annotations we would just say :


So, you can see how much of effort we just saved in writing a single line than 6 lines !!!

2. EJB 3 allows you to change stuff in your code without touching it at the time of deployment and packaging. This has nothing to do with Annotations.

3. Yes, that is correct. You can override a lot of stuff you specified by annotations using the DD. But, if you are using annotations, using xml is completely optional. Besides, there may be situations in which overriding is necessary(for security reasons for e.g.) andthats when you can use your beloved xml.

In a nutshell, Annotations help a lot by reducing the pain programmers have to go through remembering all those verbose commands in XML.

I hope this has cleared any doubt from your mind.
Good luck with EJB3 (and Annotations!!)
Keep Programming.
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Aditya,

I would like to add one more point Anagha.

Annotations are used where you won't be changing the features more often. For example, when you make a stateless bean, you won't be changing it to stateful for a long period of time unless a drastic design change takes place. So it would really save your time in using annotations for mentioning a POJO as stateless. Just use @Stateless. If not for annotations, you would have to learn all the xml elements <session> <enterprise-beans> and so on.

Use XML for frequently changing elements. For example your datasource connection URL will keep changing, from developement, to testing to production. If you use annotations you would have to recompile your classes everytime. So in such cases, it would be better to use XML.

The bottom line is annotations make your work more easier and comfortable and also help in making EJB's more attractive for learning to new people
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!