1、导入配置额外类
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类。
2、导入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配置文件。
3、Auto-configuration
Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, if
HSQLDB
is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.You need to opt-in to auto-configuration by adding the
@EnableAutoConfiguration
or@SpringBootApplication
annotations to one of your@Configuration
classes.
3.1 禁用特定的Auto-configuration类
If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of
@SpringBootApplication
to disable them, as shown in the following
如果想要禁用特定 auto-configuration 类,可以使用 @SpringBootApplication
的
exclude属性
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MyApplication {
}
3.2 逐步取代自动配置
Auto-configuration is non-invasive. At any point, you can start to define your own configuration to replace specific parts of the auto-configuration. For example, if you add your own
DataSource
bean, the default embedded database support backs away.If you need to find out what auto-configuration is currently being applied, and why, start your application with the
--debug
switch. Doing so enables debug logs for a selection of core loggers and logs a conditions report to the console.
Auto-configuration 是非侵入式的。 在任何时候都可以使用自定义的配置来取代 auto-configuration。例如,如果添加自己的DataSource bean,默认的嵌入式数据库支持就会消失。
如果需要找出当前正在应用的自动配置以及原因,请使用--debug
开关启动应用程序。这样做可以为选定的核心记录器启用调试日志,并将条件报告记录到控制台。