SpringBoot 工作机制
SpringBoot 框架是Spring框架对“约定优先于配置”理念的最佳产物。
1 . @SpringBootApplication
@SpringBootApplication 是一个“三体”结构,为一个复合注解:
@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 {
其中最为主要的三个注解为:
-
@SpringBootConfiguration
-
@EnableAutoConfiguration
-
@ComponentScan
其中@SpringBootConfiguration 本质就是@Configuration,将自己作为一个Spring容器的配置类。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {
2. @EnableAutoConfiguration
spring框架提供了许多以@Enable 开头的Annotation,我们可以称为激活模式。主要是借助@Import的支持,收集和注册特定场景的Bean定义(后面会讲到)。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
<