与 XML 配置相比,为什么 Java Config 在 Spring Boot 中更受欢迎? [英] Why Java Config is favorable in spring boot when compared to XML config?
问题描述
我是 spring-boot 的新手.我正在使用 spring-boot 构建一个基于休息的应用程序,并且正在使用 spring-security 设置安全性.我的理解是,我可以使用 xml 配置或 Java 配置来设置 spring-security.
但是,我在 spring-boot 文档中发现了以下内容.https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-configuration-classes.html
它赞成使用 Java Config 而不是 XML config.Java 配置中的更改需要重新编译.然而,它让我思考为什么文档偏爱 Java Config.
<块引用>- 配置类 Spring Boot 支持基于 Java 的配置.尽管可以使用 XML 调用 SpringApplication.run()来源,我们通常建议您的主要来源是@配置类.通常是定义main方法的类作为主要的@Configuration 也是一个不错的选择.
网上已经发布了很多Spring配置示例使用 XML 配置.总是尝试使用等效的如果可能,基于 Java 的配置.搜索启用*注释可以是一个很好的起点.
15.1 导入额外的配置类你不需要把所有的@Configuration 放到一个类中.@Import 注释可用于导入额外的配置类.或者,您可以使用@ComponentScan 自动拾取所有 Spring组件,包括@Configuration 类.
15.2 导入 XML 配置 如果您绝对必须使用基于 XML 的配置,我们建议您仍然从 @Configuration 开始类.然后,您可以使用额外的 @ImportResource 注释来加载 XML 配置文件.
有一些优点
Java 是类型安全的.如果您配置正确的 bean 类限定符,编译器将报告问题.
基于配置的 XML 可以迅速变大.[是的,我们可以拆分和导入,但仍然
搜索要简单得多,重构将是幸福的.查找 bean 定义会容易得多.
仍然有人喜欢 XML 配置并继续这样做.
有关更多信息,您可以参考 Java 配置优势 更多原因一个>
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.
- 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.
There are some advantages
Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.
XML based on configuration can quickly grow big. [Yes we can split and import but still
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
这篇关于与 XML 配置相比,为什么 Java Config 在 Spring Boot 中更受欢迎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!