Spring Boot favors Java-based configuration. Although it is possible to use SpringApplication with XML sources, we generally recommend that your primary source be a single @Configuration
class. Usually the class that defindes the main method is a good candidate as the primary @Configuration
.
Spring Boot支持基于Java的配置。虽然可以将SpringApplication与XML源一起使用,我们通常建议你的主要源是一个@Configuration
类。通常,这类中是定义了入口函数是主要的@Configuration
比较好的地方。
Many Spring configuration examples have been published on the internet that use XML configuration. If possible, always try to use the equivalent Java-based configuration. Searching for
Enable*
annotations can be a good starting point.
许多使用XML配置的Spring配置示例被发布到Internet上。如果可能,请始终尝试使用基于Java等效的配置。搜索有关
Enable*
注解是一个很好的起点。
You need not 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.
你不需要将所有的@Configuration
都放在一个类中。使用@Import
注解可以用来导入附加配置类。另外,你可以使用@ComponentScan
来自动获取所有Spring组件,包含@Configuration
类。
If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration
class. You can then use an @ImportResource
annotation to load XML configuration files.
如果你必须需要使用基于XML的配置,我们建议你仍从@Configuration
类开始。你可以使用@ImportResource
注解来加载XML配置文件。