Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would like to know why were annotations introduced in Spring and Hibernate? For earlier versions of both the frameworks book authors were saying that if we keep configuration in xml files then it will be easier to maintain (due to decoupling) and just by changing the xml file we can re-configure the application.

If we use annotations in our project and in future we want to re-configure application then again we have to compile and build the project.

So why were these annotations introduced in these frameworks? From my point of view annotations make the apps dependent on certain framework. Isn't it true?

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

For earlier versions of both the frameworks book authors were saying that if we keep configuration in xml files then it will be easier to maintain (due to decoupling) and just by changing the xml file we can re-configure the application.

Back then annotations didn't exist. People hated the XML files, and Java got a horrible reputation because of them. Heck, there was a widely used tool (XDoclet) that generated XML config files from special JavaDoc comments, because maintaining them manually was such a pain!

When annotations were introduced in Java 5, everyone quickly realized that they were for most purposes much, much more maintainable than keeping things that configure intimate details of an application (like service dependencies or DB mappings) in a separate XML files. Realistically, most of the "configuration" is really code as far as changes are concerned, i.e. changes will be made as the application is developed (not while it's deployed) and usually together with changes in the code.

If we use annotations in our project and in future we want to re-configure application then again we have to compile and build the project.

Why is that a problem? Is you build process that horrible? Maybe you should work on that then. But note that both Spring and Hibernate still allow you to use XML configuration, even in combination with annotations. You can even override existing annotations from an XML config file.

So why these annotations were introduced in these frameworks? From my point of view annotations make the apps dependent on certain framework. Isn't it true?

The app depends on the framework anyway, otherwise you wouldn't need it. So why pretend otherwise? Besides, many of the annotations are now official Java standards (such as JPA and CDI) and supported by multiple different frameworks.

share|improve this answer
    
I would also note that since annotations are placed in the code where they are needed, they reduce the the amount of "boiler plate" configuration by inferring from its surroundings and such. –  Jeremy Heiler Jun 1 '12 at 11:36
    
@Michael, thanks a lot for enlightening me. I am newbie in both the Spring and Hibernate thats why I was wondering why these two approaches were introduced. To summarize, during earlier versions annotations were not present in Java so both frameworks were using XML as there wasn't any other option. When option of annotation became available with new java XML based configuration became unnecessary. –  Chandrashekhar Jun 1 '12 at 11:57
    
@Chandrashekhar: basically yes, though there are some things that are genuinely configuration that may change between deployment sites (e.g. database server, user and password) and where it makes sense to keep them in an XML file. –  Michael Borgwardt Jun 1 '12 at 12:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.