Spring Boot 可以省掉繁琐配置文件,主要归因于它的注解。
1. @SpringBootApplication:
Spring Boot应用标注@SpringBootApplication在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就该运行这个类的main方法。

2. @SpringBootConfiguration:
Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:在配置类上标注这个注解,是原始spring的配置注解。
3. @EnableAutoConfiguration:
表示开启自动配置功能,以前spring中我们需要配置的东西,现在Spring Boot帮我们自动配置。@EnableAutoConfiguration告诉SpringBoot 开启自动配置功能,这样自动配置才可以生效。

4. @AutoConfigurationPackage:
自动配置包,在这个配置类上标注@Import({Registrar.class}),就是将主配置类(@SpringBootApplication)的所有包及下面所有子包里面的所有组件扫描到spring容器。

5. @Import({AutoConfigurationImportSelector.class})
给容器自动导入组件
AutoConfigurationImportSelector:导入哪些组件的选择器。将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中,会给容器中导入非常多的配置类(xxxAutoConfiguration),这就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
如图:自动配置类(有了这些配置类,就不需要我们手动配置了)

通过调用SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()),在Spring Boot启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效了。在Springboot中,我们以前的配置,都被这些自动配置类替换了。


上一篇:Spring Boot ——Spring Boot 项目的创建
下一篇:Spring Boot ——Spring Boot配置
网友评论