1

After having read through some of the Spring docs my inclination is towards using a XML config file rather than annotations on the classes themselves. My reasoning is that by doing so you avoid tying your POJOs to a particular framework.

Based on your experience with Spring, are there any advantages that XML configuration have over an annotation based configuration, and if not what are the disadvantages?

3

3 Answers 3

4

Many of the Spring annotations are now standardized (e.g. CDI for dependency injection), so you're not at all tying yourself to Spring by using them. And for those that aren't: your application won't work without the functionality, will it? So it's basically tied to Spring, whether or not you try to hide that fact.

Annotations are much better for maintainability because they're placed with the code they apply to, so you see them when reading the code. And they require less duplication of logic because what class they apply to is implicit in their placement, whereas XML config files always need to list the class name.

Most people prefer annotations nowadays for these reasons.

2
  • 1
    And for those that aren't: your application won't work without the functionality, will it? So it's basically tied to Spring, whether or not you try to hide that fact. Careful, dangerous amounts of common sense like that will not get you on the good side with the Ivory Tower Architects.
    – maple_shaft
    May 23, 2012 at 16:25
  • 1
    @maple_shaft careful yourself, without all that ivory tower architecture it's just moving information from A to B. You're going to hurt some egos. Jun 29, 2012 at 20:37
1

First, look at your organization. Depending on the experience and capacity to learn new things, Spring XML abstracts logic and functionality by removing the visual dependency chain from your Java files. If your development team is not accustomed to this type of development model, it can be a steep learning curve. Don't underestimate this point. I made this mistake with a group of very sharp developers and was surprised by the amount of overhead it added to the team.

I have found that using Spring Annotations, or any Java Annotations for that matter, offered significant development overhead reductions. This was due to the direct visual link they provided and seamless integration with your favorite IDEs. Having the ability to use current day-to-day tools in your development cycle like drilling down into source code, setting debugging break points, Javadoc tool-tips, etc, is not something to take for granted.

Now I know you can get a Spring Tool set for Eclipse, not sure of the others, but you are again adding a another layer of learning on your team.

0

I like using annotations for things that

  • are essential (like @Service)
  • won't change without a redeployment (like database column names)

I like using annotations for

  • global config (aspects)
  • things that could be changed by users of a component (security, role names, datasource jndi references), etc.

There is a thread at coderanch I really like on this topic. Craig Walls - the author of "Spring in Action" explains why he evolved from never using annotations to his current state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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