Using the @SpringBootApplication Annotation
很多 Spring Boot 开发人员希望让他们的应用程序使用自动配置、组件扫描,并且能够在他们的“应用程序类”上定义额外的配置。一个 @SpringBootApplication
注释可用于启用这三个特性,即:
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class".
A single @SpringBootApplication
annotation can be used to enable those three features, that is:
-
@EnableAutoConfiguration
: 启用 Spring Boot’s auto-configuration mechanism -
@EnableAutoConfiguration
: enable Spring Boot’s auto-configuration mechanism -
@ComponentScan
: 启用@Component
扫描,扫描应用程序所在的包(参见 the best practices) -
@ComponentScan
: enable@Component
scan on the package where the application is located (see the best practices) -
@SpringBootConfiguration
: 启用在上下文中注册额外的 Bean 或导入额外的配置类。Spring 标配的@Configuration
的替代产品,configuration detection 可帮助您进行集成测试。 -
@SpringBootConfiguration
: enable registration of extra beans in the context or the import of additional configuration classes. An alternative to Spring’s standard@Configuration
that aids configuration detection in your integration tests.
|
|
所有这些特性都是可选的,您可以选择用所启用的任一特性来替换此单一注释。例如,您可能不希望在应用程序中使用组件扫描或配置属性扫描: None of these features are mandatory and you may choose to replace this single annotation by any of the features that it enables. For instance, you may not want to use component scan or configuration properties scan in your application: include-code::individualannotations/MyApplication[] 在此示例中, In this example, |