1、Spring Boot应用的启动类:
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
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 { … }
2.1、`@SpringBootConfiguration`
- `@Configuration `:代表当前是一个配置类。
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
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` :标签名直译为:自动配置包,指定了默认的包规则。
- 利用Registrar给容器中导入一系列组件
- 将指定的一个包下的所有组件导入进MainApplication所在包下。