首页 > 编程语言 > 详细

《springboot》之自动装配

时间:2021-01-17 19:26:46      阅读:29      评论:0      收藏:0      [点我收藏+]

 

SpringBoot自动装配原理

从零开始开发一个Spring Boot Starter

SpringBoot启动原理分析

Spring Boot启动流程简单解释

Springboot 源码分析 —— refreshContext() 解析

 

 

 

每个SpringBoot程序都有一个主入口,也就是main方法,main里面调用SpringApplication.run()启动整个spring-boot程序

 

 

createApplicationContext

prepareContext
refreshContext
refreshContext方法中执行 invokeBeanDefinitionRegistryPostProcessors():解析配置类,生成 beanDefinitions
在refreshContext中会调用AutoConfigurationImportSelector的selectImports方法

 

该方法所在类需要使用@SpringBootApplication注解

@SpringBootApplication有很多的注解组成,其实归纳就是一个"三体"结构,重要的只有三个Annotation:

  @Configuration(@SpringBootConfiguration实质就是一个@Configuration)
  @EnableAutoConfiguration
  @ComponentScan

 

  • Configuration代表了一个配置类,相当于一个beans.xml文件
  • ComponentScan的功能其实就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中
  • EnableAutoConfiguration代表开启springboot的自动装配

 

EnableAutoConfiguration包括@Import(EnableAutoConfigurationImportSelector.class)

@Import(EnableAutoConfigurationImportSelector.class),AutoConfigurationImportSelector调用selectImports方法,selectImports方法中调用SpringFactoriesLoader.loadFactoryNames
 
其主要功能就是从指定的配置文件META-INF/spring.factories加载配置,即根据@EnableAutoConfiguration的完整类名
org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的Key,获取对应的一组@Configuration类
 
EnableutoConfiguration对应的配置项通过反射实例化为对应的标注了@Configuration的JavaConfig形式的IoC容器配置类
然后汇总为一个并加载到IoC容器。


 
 




《springboot》之自动装配

原文:https://www.cnblogs.com/zrzct/p/14286284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!