如需要此思维导图可去自行下载
链接:https://pan.baidu.com/s/1Jnj7j4aeGIRxKathP_I9Yw
提取码:9vkn
@SpringBootApplication注解下有三个核心注解,分别是
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
1.@SpringBootConfiguration
底层注解使用了@Configuration,
<在https://blog.csdn.net/qq_42480555/article/details/118359107?spm=1001.2014.3001.5501用纯注解方式整合dbutils时候讲过此注解,可点击查看标志当前类为IOC容器的配置类用法>
相当于在配置文件xml中配置的bean,SpringBootConfiguration注解会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。
例如springboot整合的redis操作类的配置类:
2.@ComponentScan
扫描当前工程的java类,交给IOC容器,相当于之前<context:component-scan base-package=""/>包扫描
3.@EnableAutoConfiguration
此注解下有两个核心注解:
- @AutoConfigurationPackage
- @Import(AutoConfigurationImportSelector.class)
@AutoConfigurationPackage
获取当前启动类的包名,SpringBoot应用启动时默认会将启动类所在的package作为自动配置的package。
@Import(AutoConfigurationImportSelector.class)
AutoConfigurationImportSelector.class里实现了ImportSelector,内部扫描spring.factories文件,spring.factories如下:
获取springboot整合的所有全限定名,然后通过反射将所需对象加入到IOC容器(并不是将spring.factories所有字节码对象加载到IOC容器中,里面有个过滤机制,会根据当前项目的需要导入我们需要的对象)
这个注解作用就是将springboot整合后的操作类对象注入到IOC容器中。