springboot自动装配原理初识

2025-05-29 0 57

运行原理

为了研究,我们正常从父项目的pom.xml开始进行研究。

pom.xml

父依赖 spring-boot-starter-parent主要用来管理项目的资源过滤和插件

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.2.5.RELEASE</version>
  5. <relativePath/> <!– lookup parent from repository –>
  6. </parent>

点父依赖进去查看,发现还有一个父依赖spring-boot-dependencies,这里的这个父依赖才是真正管理springboot应用里面的所有依赖版本的地方,是springboot的版本控制中心。

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-dependencies</artifactId>
  4. <version>2.2.5.RELEASE</version>
  5. <relativePath>../../spring-boot-dependencies</relativePath>
  6. </parent>

启动器:spring-boot-starter-xxx:springboot的场景启动器

spring-boot-starter-web:导入web依赖的组件

  1. <dependency> <groupId>org.springframework.boot</groupId>
  2. <artifactId>spring-boot-starter-web</artifactId>
  3. </dependency>

主程序

@SpringBootApplication

作用:标注这是一个springboot主程序类,说明这是一个springboot应用,springboot就是运行这个类的mian方法启动的springboot应用。

  1. @SpringBootApplication //标注这是一个主程序类,说明这是一个springboot应用
  2. public class Springboot01HelloworldApplication {
  3. public static void main(String[] args) {
  4. //这里启动了一个服务,而不是执行了一个方法。
  5. SpringApplication.run(Springboot01HelloworldApplication.class, args);
  6. }
  7. }

点@SpringBootApplication继续研究,会发现有@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan这三个注解

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan(
  4. excludeFilters = {@Filter(
  5. type = FilterType.CUSTOM,
  6. classes = {TypeExcludeFilter.class}
  7. ), @Filter(
  8. type = FilterType.CUSTOM,
  9. classes = {AutoConfigurationExcludeFilter.class}
  10. )}
  11. )

1.@ComponentScan: spring自动扫描包

这个我们在spring配置文件中见到过,它用来自动扫描并加载符合条件的组件或者bean,并将bean加载到IOC容器中。

2.@SpringBootConfiguration: springboot的配置类

标注在某个类上,说明这个类是springboot的配置类,在这里它就说明SpringBootApplication这个类是springboot的配置类。

我们继续点@SpringBootConfiguration进去查看,会发现 @Configuration这个注解

2.1 @Configuration:配置类,用来配置spring的xml文件

我们继续点@Configuration进去查看,会发现 @Component这个注解。

2.2 @Component:组件,说明启动类本身也是一个组件,负责启动应用。

至此,@SpringBootConfiguration这条线,我们研究完了。

3.@EnableAutoConfiguration:开启自动装配,通过@EnableAutoConfiguration来帮我们自动配置之前我们需要配置的东西。
我们继续点@EnableAutoConfiguration进去查看,会发现 @AutoConfigurationPackage和@Import({AutoConfigurationImportSelector.class}) 这两个注解。

  1. @Target({ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @AutoConfigurationPackage
  6. @Import({AutoConfigurationImportSelector.class})
  7. public @interface EnableAutoConfiguration {
  8. String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
  9. Class<?>[] exclude() default {};
  10. String[] excludeName() default {};
  11. }

3.1 @AutoConfigurationPackage自动装配

继续点进去查看,出现@Import({Registrar.class})这个注解

3.1.1 @Import({Registrar.class}): spring底层注解,给容器导入一个组件

Registrar.class: 将主启动类所在包及所在包下面的所有子包里面所有的组件都扫描到Spring容器。

至此,@AutoConfigurationPackage这条线我们也研究完了。

3.2 @Import({AutoConfigurationImportSelector.class}): 给容器导入组件

AutoConfigurationImportSelector.class:自动装配导入选择器。

导入的选择器分析:

1.我们点进去AutoConfigurationImportSelector.class这个类的源码进行探究,

springboot自动装配原理初识

2.我们点击getCandidateConfigurations进一步分析

  1. protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
  2. List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
  3. Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
  4. return configurations;
  5. }

2.1 使用了getSpringFactoriesLoaderFactoryClass()方法,返回一开始我们看到的启动自动配置文件的注解类EnableAutoConfiguration.class

  1. protected Class<?> getSpringFactoriesLoaderFactoryClass() {
  2. return EnableAutoConfiguration.class;
  3. }

2.2 发现它调用了SpringFactoriesLoader类的静态方法,我们点击loadFactoryNames进入loadFactoryNames()

  1. public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
  2. ClassLoader classLoaderToUse = classLoader;
  3. if (classLoader == null) {
  4. classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
  5. }
  6. String factoryTypeName = factoryType.getName();
  7. return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
  8. }

发现它又调用了loadSpringFactories()方法,点进去查看

  1. private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
  2. Map<String, List<String>> result = (Map)cache.get(classLoader);
  3. if (result != null) {
  4. return result;
  5. } else {
  6. HashMap result = new HashMap();
  7. try {
  8. Enumeration urls = classLoader.getResources("META-INF/spring.factories");
  9. while(urls.hasMoreElements()) {
  10. URL url = (URL)urls.nextElement();
  11. UrlResource resource = new UrlResource(url);
  12. Properties properties = PropertiesLoaderUtils.loadProperties(resource);
  13. Iterator var6 = properties.entrySet().iterator();
  14. while(var6.hasNext()) {
  15. Entry<?, ?> entry = (Entry)var6.next();
  16. String factoryTypeName = ((String)entry.getKey()).trim();
  17. String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
  18. String[] var10 = factoryImplementationNames;
  19. int var11 = factoryImplementationNames.length;
  20. for(int var12 = 0; var12 < var11; ++var12) {
  21. String factoryImplementationName = var10[var12];
  22. ((List)result.computeIfAbsent(factoryTypeName, (key) -> {
  23. return new ArrayList();
  24. })).add(factoryImplementationName.trim());
  25. }
  26. }
  27. }
  28. result.replaceAll((factoryType, implementations) -> {
  29. return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  30. });
  31. cache.put(classLoader, result);
  32. return result;
  33. } catch (IOException var14) {
  34. throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
  35. }
  36. }
  37. }

源码分析:

  1. MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);获得classLoader,我们返回可以看到这里得到的就是EnableAutoConfiguration标注的类本身
  2. Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");获取一个资源 "META-INF/spring.factories"
  3. while循环,读取到的资源遍历,封装成为一个Properties

spring.factories文件

springboot自动装配原理初识

WebMvcAutoConfiguration

我们在上面的自动配置类随便找一个打开看看,比如 :WebMvcAutoConfiguration

springboot自动装配原理初识

都是大家熟悉的配置,所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件 ,并将其中对应的 org.springframework.boot.autoconfigure. 包下的配置项,通过反射实例化为对应标注了 @Configuration的JavaConfig形式的IOC容器配置类 , 然后将这些都汇总成为一个实例并加载到IOC容器中。

总结

  1. SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值
  2. 将这些值作为自动配置类导入容器,自动配置类就生效,帮我们进行自动配置工作;
  3. 整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中;
  4. 它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;
  5. 有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;

主启动类

SpringApplication

  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3. public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args);
  4. }
  5. }

分析:

  1. SpringbootApplication.class:应用参数的入口
  2. args:命令行参数
  3. 该方法返回的是一个ConfigurableApplicationContext对象

SpringApplication主要做的事情:

  1. 推断应用的类型是普通的项目还是Web项目
  2. 查找并加载所有可用初始化器 , 设置到initializers属性中
  3. 找出所有的应用程序监听器,设置到listeners属性中
  4. 推断并设置main方法的定义类,找到运行的主类

以上就是springboot自动装配原理初识的详细内容,更多关于springboot自动装配原理的资料请关注快网idc其它相关文章!

原文链接:https://juejin.cn/post/6947568989815963661

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 springboot自动装配原理初识 https://www.kuaiidc.com/107532.html

相关文章

发表评论
暂无评论