@SpringBootApplication注解

多个注解的集合
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

  1. package org.springframework.boot.autoconfigure;
  2. @Target(ElementType.TYPE)
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Documented
  5. @Inherited
  6. @SpringBootConfiguration
  7. @EnableAutoConfiguration
  8. @ComponentScan(excludeFilters = {
  9. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  10. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  11. public @interface SpringBootApplication {
  12. ......
  13. }
  14. package org.springframework.boot;
  15. @Target(ElementType.TYPE)
  16. @Retention(RetentionPolicy.RUNTIME)
  17. @Documented
  18. @Configuration
  19. public @interface SpringBootConfiguration {
  20. }

根据 SpringBoot 官网,这三个注解的作用分别是:

  • @EnableAutoConfiguration:启用 SpringBoot 的自动配置机制
  • @ComponentScan: 扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。
  • @Configuration:允许在 Spring 上下文中注册额外的 bean 或导入其他配置类