@SpringBootApplication理解

妖狐艹你老母 2022-06-16 00:25 384阅读 0赞

@SpringBootApplication是Spring Boot 的核心注解,它是一个组合注解,源码如下:

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @SpringBootConfiguration
  6. @EnableAutoConfiguration
  7. @ComponentScan(excludeFilters = {
  8. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  10. public @interface SpringBootApplication {
  11. /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */
  12. @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
  13. Class<?>[] exclude() default {};
  14. /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */
  15. @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
  16. String[] excludeName() default {};
  17. /** * Base packages to scan for annotated components. Use { @link #scanBasePackageClasses} * for a type-safe alternative to String-based package names. * @return base packages to scan * @since 1.3.0 */
  18. @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
  19. String[] scanBasePackages() default {};
  20. /** * Type-safe alternative to { @link #scanBasePackages} for specifying the packages to * scan for annotated components. The package of each class specified will be scanned. * <p> * Consider creating a special no-op marker class or interface in each package that * serves no purpose other than being referenced by this attribute. * @return base packages to scan * @since 1.3.0 */
  21. @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
  22. Class<?>[] scanBasePackageClasses() default {};
  23. }

这里有两个作用

  1. 自动依赖,就像图中所示的,
    这里写图片描述
    可以看到只使引入了一个web,但是它会自动为我们引入starter和tomcat等内容,这就是@EnableAutoConfiguration所起的作用
  2. 自动扫描@SpringBootApplication所在类的同级包,如图所示:
    这里写图片描述
    Ch523Application类使Spring Boot的一个入口类,里面有@SpringBootApplication注解,有一个main方法,这个方法就是普通的java main方法,和这个类同级别的包都可以自动的扫描,但是不在这个包里的类就不会扫描到,就像这个图片中的com.wisely.test这个包,这里的类就不会被扫描到spring的容器中。

发表评论

表情:
评论列表 (有 0 条评论,384人围观)

还没有评论,来说两句吧...

相关阅读