Configuration Classes

Spring Boot 偏好基于 Java 的配置。虽然可以使用 SpringApplication 与 XML 源一起使用,但我们通常建议你的主要源是一个 @Configuration 类。通常,定义 main 方法的类是作为主要 @Configuration 的一个好候选者。

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 defines the main method is a good candidate as the primary @Configuration.

网上发表了很多 Spring 配置示例,这些示例使用 XML 配置。如果可能,请始终尝试使用等效的基于 Java 的配置。搜索 Enable* 注解可能是一个好的起点。

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.

Importing Additional Configuration Classes

你无需将所有 @Configuration 都放入一个类中。可以 @Import 注解用于导入其他配置类。或者,你可以使用 @ComponentScan 自动拾取所有 Spring 组件,包括 @Configuration 类。

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.

Importing XML Configuration

如果确实必须使用基于 XML 的配置,我们建议你仍然从 @Configuration 类开始。然后你可以使用 @ImportResource 注解加载 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.