zl程序教程

您现在的位置是:首页 >  后端

当前栏目

基础springboot扫描讲解

SpringBoot基础 讲解 扫描
2023-09-11 14:16:27 时间

基础springboot扫描讲解

springboot就是一个第三方提供的一个自动装配框架。可以快速的帮助开发,构建架构,Tomcat内置运行,打包完成后为一个jar,java -jar springbootXXXXXX.jar 就可以运行。

springboot自动装配的原理,创建一个main入口,在类上注解@SpringBootApplication,这就完成了springboot的基本配置,前提是pom.xml配置好依赖

在这里插入图片描述

@SpringBootApplication是如何进行自动装配?
点击@SpringBootApplication
在这里插入图片描述
进入@SpringBootApplication注解,我们可以看到有几个@注解:
@Target,@Retention,@Documented,@Inherited这几个注解不多说
主要讲:@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan

点击进入@SpringBootConfiguration
在这里插入图片描述
看到什么,@configuration,说明什么,说明@SpringBootApplication本身就是一个ioc容器配置类,可以进行自动装配,在写代码时配置的@Controller,@Service,@Component,@Repository自动扫描配置到ioc容器中,供springboot调用。

进入@EnableAutoConfiguration
在这里插入图片描述
@AutoConfigurationPackage:自动配置包,扫描指定的package包
@Import:导入.class,将类导入ioc容器
@Import导入了一个AutoConfigurationImportSelector.class,AutoConfigurationImportSelector有一个方法selectImports(),这个方法是返回的数组中是需要自动装配的全限定类名, 底层会根据数组中的全限定类名将这些类进行注入.
在这里插入图片描述
selectImports下面有一个方法List configurations = getCandidateConfigurations(annotationMetadata,
attributes);进入getCandidateConfigurations,可以看它去找配置文件
在这里插入图片描述
表面了在orgspringframeworkootspring-boot-autoconfigure.0.2.RELEASEspring-boot-autoconfigure-2.0.2.RELEASE.jar ,META-INF/spring.factories有一个配置文件
在这里插入图片描述
springboot就扫描这个配置文件下的包名,进行自动装配,注入ioc。