微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

与XML配置相比,为什么Java 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配置. Java配置中的更改需要重新编译.然而,它让我想到为什么文档支持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.

解决方法:

有一些优点

> Java是类型安全的.如果要配置正确的bean类限定符,编译器将报告问题.
>基于配置的XML可以快速增长. [是的,我们可以拆分和进口,但仍然
>搜索更简单,重构将是幸福.查找bean定义将更容易.

仍然有人喜欢XML配置并继续这样做.

有关详细信息,请参阅Java configuration advantages Some more reasons

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。