I'm new to Spring and DI in general. But from what I have read, DI affords you the ability to swap out implementations very easily using frameworks like Spring
. I can understand value there when it comes to XML bean configuration because code doesn't need to be changed at all for accomplishing switchable implementations. But if we're using annotations like @Autowired
or @Qualifier
...we would need to change the code. So why do we want to use annotations over XML-based configuration?

- 438
- 1
- 5
- 17
-
Or just swap a library out, without changing any XML... – Boris the Spider Apr 02 '18 at 19:43
-
3You're still changing code. XML is still code. The configuration is still code that needs to be validated. – Makoto Apr 02 '18 at 19:45
-
If you don't want your code depends on Spring, Use JSR-330 Annotations frm Java EE6, Refer this example - https://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/ – Sundararaj Govindasamy Apr 02 '18 at 19:55
-
There are lots of debates about why we should use annotations. About your question, annotations don't solve what you want to achieve. You still have to update annotation same as XML if you want to swap. If you don't want to touch neither code and xml, you could check this https://stackoverflow.com/questions/4041300/can-i-replace-a-spring-bean-definition-at-runtime – AntonIva Apr 02 '18 at 19:56
-
@AntonIva That approach is seriously obsolete. – chrylis -cautiouslyoptimistic- Apr 02 '18 at 19:57
-
@chrylis, got it – AntonIva Apr 02 '18 at 20:34
-
1These days, just go ahead and use code-based configuration. Especially if you're writing a library to be consumed by some downstream application, the courteous approach is to provide a Spring Boot autoconfiguration class so that everything Just Works. – chrylis -cautiouslyoptimistic- Apr 02 '18 at 20:35
2 Answers
This was actually a topic of lively conversation around the time that Spring 3.0 came out in 2009 and JavaConfig was added to the core system base.
In theory, being able to externalize application setup is a great thing. However, it turns out that in practice there are two distinct groups of setup choices: the application shape or dependency graph, and particular values like API keys, database connection strings, and so on that vary between environment but usually don't change the way that the beans are wired.
Experience has shown that the dependency graph, which is essentially what you'd express in XML, is almost never changed without also making changes to the accompanying implementation code anyway, so there is very little real-world benefit to defining the graph in XML. On the other hand, writing @Bean
methods in Java means that it's a lot easier to test configuration when needed, the compiler can ensure type safety, and decision logic (such as conditionals) are simpler to implement.
Furthermore, the availability of annotations means that it's possible to extend the domain-specific language for configuration fairly easily in Java--just create a new annotation and its accompanying processor (such as @ConditionalOnProperty
); Spring Boot itself is an extreme example of the flexibility of this model. In XML, on the other hand, injecting new tags or attributes into a schema is a lot more of a hassle.
There are times when XML may still be the better choice (I've particularly gone with it for writing out Spring Integration pipelines, which can be easier to read in XML than in the Java DSL), but the real-world benefits turned out not to be all that valuable in most cases, and the safety and flexibility of configuration as code has won out.

- 75,269
- 21
- 115
- 152
This is not must, you have to choices to use Annotations or XML its up to you but Annotations is easy to use, faster and more readable than configurations, and when you use it you will find all the information in a single file, but it also have disadvantages.
Annotations preferable
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.
I recommended you to read the following links for more info :
xml-configuration-versus-annotation-based-configuration
spring-framework-xml-vs-annotations

- 2,248
- 3
- 23
- 39