Over a million developers have joined DZone.

Spring Framework – XML vs. Annotations

Some rules of thumb on when to use either XML or annotation based configurations

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

This question has been around for many years since Spring started to move heavily towards Annotation based configurations (if I recall, it’s called configuration by convention). Annotations based configurations were like a jungle fire that spread across the industry and very soon it was the norm. But, this question “XML vs. Annotations” always existed.

I for one have been around the Spring world since version 1.1 when annotations weren’t a thing. I know how write XML and I know how to configure an application to suit my needs. Since then, whenever I write an application in Spring, I have asked myself "XML or Annotations" and I never really had a good answer until recently. While you will find tons of posts around this topic in Google,  when you search for this, only a few really give you an unbiased opinion.

I started to work on an application that needs some very flexible configuration options and before I dive into that I had to make this decision yet again, and this time I wanted to keep things simple and my rationale was…

  • Use Annotations in anything that is stable and defines the core structure of the application. Anything that would need a code change is okay to sit as an annotation.

Use XML based configurations when you know you may have a need to alter the behavior of an application without the need of compiling and deploying the code all over again.

This is how simple I kept it for my team. Once this principle is defined, job is only halve done. But we will get there soon.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
spring ,java

Published at DZone with permission of Kapil Viren Ahuja, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}