Skip to content

Files

Latest commit

96a7ff3 · May 24, 2018

History

History
31 lines (16 loc) · 2 KB

15.ConfigurationClasses.md

File metadata and controls

31 lines (16 loc) · 2 KB

15. Configuration Class

15. 配置类

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比较好的地方。

Tip
小提示

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*注解是一个很好的起点。

Importing Additional Configuration Classes

导入附加配置类

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类。

Imporing XML Configuration

导入XML配置

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配置文件。