广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot自动装配原理初识
  • 363
分享到

springboot自动装配原理初识

2024-04-02 19:04:59 363人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录运行原理 pom.xml 主程序 导入的选择器分析:spring.factories文件总结 主启动类 运行原理 为了研究,我们正常从父项目的pom.xml开始进行研究。 po

运行原理

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

pom.xml

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


<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

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


<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.2.5.RELEASE</version>
  <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

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

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


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

主程序

@SpringBootApplication

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


@SpringBootApplication //标注这是一个主程序类,说明这是一个springboot应用
public class Springboot01HelloworldApplication {

  public static void main(String[] args) {
    //这里启动了一个服务,而不是执行了一个方法。
    SpringApplication.run(Springboot01HelloworldApplication.class, args);
  }
}

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


@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
  excludeFilters = {@Filter(
  type = FilterType.CUSTOM,
  classes = {TypeExcludeFilter.class}
), @Filter(
  type = FilterType.CUSTOM,
  classes = {AutoConfigurationExcludeFilter.class}
)}
)

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}) 这两个注解。


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
  String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

  Class<?>[] exclude() default {};

  String[] excludeName() default {};
}

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这个类的源码进行探究,

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


  protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    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.");
    return configurations;
  }

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


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

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


public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    ClassLoader classLoaderToUse = classLoader;
    if (classLoader == null) {
      classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
    }

    String factoryTypeName = factoryType.getName();
    return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
  }

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


  private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
    Map<String, List<String>> result = (Map)cache.get(classLoader);
    if (result != null) {
      return result;
    } else {
      HashMap result = new HashMap();

      try {
        Enumeration urls = classLoader.getResources("META-INF/spring.factories");

        while(urls.hasMoreElements()) {
          URL url = (URL)urls.nextElement();
          UrlResource resource = new UrlResource(url);
          Properties properties = PropertiesLoaderUtils.loadProperties(resource);
          Iterator var6 = properties.entrySet().iterator();

          while(var6.hasNext()) {
            Entry<?, ?> entry = (Entry)var6.next();
            String factoryTypeName = ((String)entry.geTKEy()).trim();
            String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
            String[] var10 = factoryImplementationNames;
            int var11 = factoryImplementationNames.length;

            for(int var12 = 0; var12 < var11; ++var12) {
              String factoryImplementationName = var10[var12];
              ((List)result.computeIfAbsent(factoryTypeName, (key) -> {
                return new ArrayList();
              })).add(factoryImplementationName.trim());
            }
          }
        }

        result.replaceAll((factoryType, implementations) -> {
          return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
        });
        cache.put(classLoader, result);
        return result;
      } catch (IOException var14) {
        throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
      }
    }
  }

源码分析

  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文件

WebmvcAutoConfiguration

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

都是大家熟悉的配置,所以,自动配置真正实现是从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


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

分析:

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

SpringApplication主要做的事情:

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

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

--结束END--

本文标题: springboot自动装配原理初识

本文链接: https://www.lsjlt.com/news/122897.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作