基础springboot扫描讲解

基础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。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值