1、Spring Boot应用的启动类:

    1. @SpringBootApplication
    2. public class MainApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(MainApplication.class, args);
    5. }
    6. }

    4.1、自动包规则原理 - 图1
    2、分析下 @SpringBootApplication 注解:

    • 重点分析:@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan。 ```java @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication { … }
    1. 2.1`@SpringBootConfiguration`
    2. - `@Configuration `:代表当前是一个配置类。
    3. ```java
    4. @Target(ElementType.TYPE)
    5. @Retention(RetentionPolicy.RUNTIME)
    6. @Documented
    7. @Configuration
    8. public @interface SpringBootConfiguration {
    9. @AliasFor(
    10. annotation = Configuration.class
    11. )
    12. boolean proxyBeanMethods() default true;
    13. }

    2.2、@ComponentScan :指定扫描哪些Spring注解。

    2.3、@EnableAutoConfiguration

    • 重点分析@AutoConfigurationPackage@Import(AutoConfigurationImportSelector.class)。 ```java @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = “spring.boot.enableautoconfiguration”;

      Class<?>[] exclude() default {};

      String[] excludeName() default {}; }

    `` 2.3.1、@AutoConfigurationPackage` :标签名直译为:自动配置包,指定了默认的包规则。

    1. 利用Registrar给容器中导入一系列组件
    2. 将指定的一个包下的所有组件导入进MainApplication所在包下。