3
votes

I am new to spring-boot. I am building a rest based application using spring-boot and was working on setting up security using spring-security. It is my understnading that I can setup spring-security with either xml config or with Java config.

However, I found the following in spring-boot documentation. https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-configuration-classes.html

It is in favor of using Java Config as opposed to XML config. Changes in Java config requires recompilation. Yet, it makes me think why the documentation favors Java Config.

  1. Configuration classes Spring Boot favors Java-based configuration. Although it is possible to call SpringApplication.run() with an XML source, we generally recommend that your primary source is a @Configuration class. Usually the class that defines the main method is also a good candidate as the primary @Configuration.

Many Spring configuration examples have been published on the Internet that use XML configuration. Always try to use the equivalent Java-based configuration if possible. Searching for Enable* annotations can be a good starting point.

15.1 Importing additional configuration classes You don’t need to put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

15.2 Importing XML configuration If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an additional @ImportResource annotation to load XML configuration files.

1
xml config is good approach. Why? because you no need to re-compiled java code. just change the config and restart the server. @Configuration is where the configuration inside the code. if you change some value, you need to re-compile -> deployed -> start server. - Sh4m
@Sh4m I agree with you. Only reason I got this question is because when I was looking at examples online for spring-boot, I always saw the configuration being managed in java class rather than xml. Hence, I started thinking this way and wanted to make sure what is right. - SaAn
Nothing wrong with XML config. Its popularity has waned recently in favor of Java Config and Annotations but that's generally just a personal preference. - Kyle Anderson
Unless your XML configuration is external to your application (which would be a bad thing imho) you still need to rebuild your artifact and have it tested etc. Using java or xml doesn't change this. Also with Java based configuration you can use all the nice things java gives you for configuring your application (type safety, if/else, switches etc. etc.) not to mention the new conditional bean support which is far superior in Java then in xml. - M. Deinum

1 Answers

6
votes

There are some advantages

  1. Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.

  2. XML based on configuration can quickly grow big. [Yes we can split and import but still

  3. Search is much simpler, refactoring will be bliss. Finding a bean definition will be far easier.

There are still people who like XML configuration and continue to do it.

For more info you can refer Java configuration advantages Some more reasons