Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Framework – XML vs. Annotations

DZone's Guide to

Spring Framework – XML vs. Annotations

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

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}