springboot 提供了@SpringBootApplication注解,用来标注是springboot启动项目,该注解为合成注解,即@SpringBootApplication=@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan三个注解。
官网给出的定义:
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: enable Spring Boot’s auto-configuration mechanism
@ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
@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.
许多的springboot开发者喜欢在他们的应用类中去使用auto-configuration、auto-configuration scan 和能够定义一些额外的配置,一个@SpringBootApplication注解能够包含三个特征,分别是:
@EnableAutoConfiguration:能够开启springboot的自动配置机制
@ComponentScan: 能够扫描应用所在的包
@SpringBootConfiguration:能够在上下文中注册额外的类或者引入添加的配置类
Spring 标准 @Configuration 的替代方案,可帮助您在集成测试中进行配置检测
默认情况下,扫描范围为启动类所在包下的范围,例如
图片中的MyApplication 使用了@SpringBootApplication注解后,扫描范围是:com.lixl.demo.application但是不会扫描com.lixl.demo.other包。
同时springboot也提供了解决办法:使用@ComponentScan注解进行扫描范围设置