Auto-configuration
Spring Boot 自动配置尝试基于您添加的 jar 依赖自动配置您的 Spring 应用程序。例如,如果`HSQLDB`位于您的类路径中,并且您没有手动配置任何数据库连接 bean,那么 Spring Boot 自动配置一个内存数据库。
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.
您需要通过将`@EnableAutoConfiguration` 或`@SpringBootApplication`注释添加到一个您的`@Configuration` 类来选择加入自动配置。
You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration
or @SpringBootApplication
annotations to one of your @Configuration
classes.
您应该只添加一个`@SpringBootApplication` 或`@EnableAutoConfiguration` 注释。我们通常建议您只将其中一个添加到您的主`@Configuration`类。 |
You should only ever add one |
Gradually Replacing Auto-configuration
自动配置是非侵入性的。在任何时候,您都可以开始定义自己的配置来替换自动配置的特定部分。例如,如果您添加了自己的`DataSource` bean,则默认嵌入式数据库支持会回退。
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.
如果您需要了解当前正在应用哪些自动配置以及原因,请使用 --debug
开关启动您的应用程序。这样做会启用核心记录器的一组调试日志,并将条件报告记录到控制台。
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.
Disabling Specific Auto-configuration Classes
如果您发现正在应用不想要的特定自动配置类,可以使用`@SpringBootApplication`的 exclude 属性禁用它们,如下例所示:
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 example:
如果该类不在类路径中,可以使用注释的`excludeName`属性并指定完全限定名称。如果您更喜欢使用`@EnableAutoConfiguration` 而不是`@SpringBootApplication`,`exclude`和`excludeName`也是可用的。最后,您还可以使用 configprop:spring.autoconfigure.exclude[] 属性控制要排除的自动配置类列表。
If the class is not on the classpath, you can use the excludeName
attribute of the annotation and specify the fully qualified name instead.
If you prefer to use @EnableAutoConfiguration
rather than @SpringBootApplication
, exclude
and excludeName
are also available.
Finally, you can also control the list of auto-configuration classes to exclude by using the configprop:spring.autoconfigure.exclude[] property.
您可以在注释级别和使用属性同时定义排除项。 |
You can define exclusions both at the annotation level and by using the property. |
即使自动配置类是`public`,该类的唯一被认为是公共 API 的方面是可用于禁用自动配置的类名。这些类的实际内容,例如嵌套配置类或 bean 方法仅供内部使用,我们不建议直接使用它们。 |
Even though auto-configuration classes are |
Auto-configuration Packages
自动配置软件包是各种自动配置功能在默认情况下扫描项目(如实体和 Spring Data 存储库)时查找的软件包。@EnableAutoConfiguration`注释(直接或通过其存在于
@SpringBootApplication`上)确定默认自动配置软件包。可以使用`@AutoConfigurationPackage`注释配置其他软件包。
Auto-configuration packages are the packages that various auto-configured features look in by default when scanning for things such as entities and Spring Data repositories.
The @EnableAutoConfiguration
annotation (either directly or through its presence on @SpringBootApplication
) determines the default auto-configuration package.
Additional packages can be configured using the @AutoConfigurationPackage
annotation.