zxpnet网站 zxpnet网站
首页
前端
后端服务器
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

zxpnet

一个爱学习的java开发攻城狮
首页
前端
后端服务器
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 大后端课程视频归档
  • 南航面试题
  • 并发编程

  • 性能调优

  • java8语法

  • lombok

  • 日志

  • 工具类

  • spring

  • mybatis

  • springboot

    • springboot基础篇
    • springboot配置文件yaml
    • springboot web开发
    • springboot数据访问
    • springboot单元测试
    • springboot性能监控actuator
    • SpringBoot 原理解析
    • spring.factories文件
      • springboot常用注解
      • springboot序列化
      • springboot整合swagger
      • Spring Boot 定制URL匹配规则的方法
      • springboot整合redisson分布式锁
      • springboot+线程池使用
      • springboot2应用中碰到的一些坑
      • SpringBoot面试题
    • redis

    • zookeeper

    • springcloud

    • dubbo

    • netty

    • springsecurity

    • mq消息中间件

    • shiro

    • beetle

    • 模板引擎

    • jpa

    • 数据结构与算法

    • 数据库知识与设计

    • gradle

    • maven

    • bus

    • 定时任务

    • docker

    • centos

    • 加解密

    • biz业务

    • pigx项目

    • 开源项目

    • 品达通用权限项目-黑马

    • 货币交易项目coin-尚学堂

    • php

    • backend
    • springboot
    shollin
    2021-06-16
    目录

    spring.factories文件

    • Spring Boot的扩展机制之Spring Factories
    • 什么是 SPI机制
    • Spring Boot中的SPI机制
    • Spring Factories实现原理是什么
    • Spring Factories在Spring Boot中的应用
    • ApplicationListener的应用

    # Spring Boot的扩展机制之Spring Factories

    • Spring Boot中有一种非常解耦的扩展机制:Spring Factories。这种扩展机制实际上是仿照Java中的SPI扩展机制来实现的。

    • spring.factories配置文件的作用总结起来就是帮助spring-boot项目包以外的bean(即在pom文件中添加依赖中的bean)注册到spring-boot项目的spring容器。

    # 什么是 SPI机制

    SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的。在java.util.ServiceLoader的文档里有比较详细的介绍。 简单的总结下java SPI机制的思想。我们系统里抽象的各个模块,往往有很多不同的实现方案,比如日志模块的方案,xml解析模块、jdbc模块的方案等。面向的对象的设计里,我们一般推荐模块之间基于接口编程,模块之间不对实现类进行硬编码。一旦代码里涉及具体的实现类,就违反了可拔插的原则,如果需要替换一种实现,就需要修改代码。为了实现在模块装配的时候能不在程序里动态指明,这就需要一种服务发现机制。 java SPI就是提供这样的一个机制:为某个接口寻找服务实现的机制。有点类似IOC的思想,就是将装配的控制权移到程序之外,在模块化设计中这个机制尤其重要。

    # Spring Boot中的SPI机制

    在Spring中也有一种类似与Java SPI的加载机制。它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。 这种自定义的SPI机制是Spring Boot Starter实现的基础。

    # Spring Factories实现原理是什么

    spring-core包里定义了SpringFactoriesLoader类,这个类实现了检索META-INF/spring.factories文件,并获取指定接口的配置的功能。在这个类中定义了两个对外的方法:

    • loadFactories 根据接口类获取其实现类的实例,这个方法返回的是对象列表。
    • loadFactoryNames 根据接口获取其接口类的名称,这个方法返回的是类名的列表。 上面的两个方法的关键都是从指定的ClassLoader中获取spring.factories文件,并解析得到类名列表,具体代码如下↓
     private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
            MultiValueMap<String, String> result = cache.get(classLoader);
            if (result != null) {
                return result;
            }
    
            try {
                Enumeration<URL> urls = (classLoader != null ?
                        classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
                        ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
                result = new LinkedMultiValueMap<>();
                while (urls.hasMoreElements()) {
                    URL url = urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    for (Map.Entry<?, ?> entry : properties.entrySet()) {
                        String factoryClassName = ((String) entry.getKey()).trim();
                        for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
                            result.add(factoryClassName, factoryName.trim());
                        }
                    }
                }
                cache.put(classLoader, result);
                return result;
            }
            catch (IOException ex) {
                throw new IllegalArgumentException("Unable to load factories from location [" +
                        FACTORIES_RESOURCE_LOCATION + "]", ex);
            }
        }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    从代码中我们可以知道,在这个方法中会遍历整个ClassLoader中所有jar包下的spring.factories文件。也就是说我们可以在自己的jar中配置spring.factories文件,不会影响到其它地方的配置,也不会被别人的配置覆盖。

    spring.factories的是通过Properties解析得到的,所以我们在写文件中的内容都是安装下面这种方式配置的:

    org.springframework.boot.diagnostics.FailureAnalyzer=\
    com.pig4cloud.pigx.common.gateway.exception.RouteCheckFailureAnalyzer
    
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
        com.pig4cloud.pigx.common.data.cache.RedisTemplateConfig,\
        com.pig4cloud.pigx.common.data.cache.RedisCacheManagerConfig,\
        com.pig4cloud.pigx.common.data.cache.RedisCacheAutoConfiguration,\
        com.pig4cloud.pigx.common.data.tenant.PigxTenantConfigProperties,\
        com.pig4cloud.pigx.common.data.tenant.TenantContextHolderFilter,\
        com.pig4cloud.pigx.common.data.tenant.PigxTenantConfiguration,\
        com.pig4cloud.pigx.common.data.mybatis.MybatisPlusConfiguration,\
        com.pig4cloud.pigx.common.data.resolver.TenantKeyStrResolver
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    如果一个接口希望配置多个实现类,可以使用’,’进行分割。

    # Spring Factories在Spring Boot中的应用

    在Spring Boot的很多包中都能够找到spring.factories文件,接下来我们以spring-boot包为例进行介绍

    # PropertySource Loaders
    org.springframework.boot.env.PropertySourceLoader=\
    org.springframework.boot.env.PropertiesPropertySourceLoader,\
    org.springframework.boot.env.YamlPropertySourceLoader
    
    # Run Listeners
    org.springframework.boot.SpringApplicationRunListener=\
    org.springframework.boot.context.event.EventPublishingRunListener
    
    # Error Reporters
    org.springframework.boot.SpringBootExceptionReporter=\
    org.springframework.boot.diagnostics.FailureAnalyzers
    
    # Application Context Initializers
    org.springframework.context.ApplicationContextInitializer=\
    org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
    org.springframework.boot.context.ContextIdApplicationContextInitializer,\
    org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
    org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
    
    # Application Listeners
    org.springframework.context.ApplicationListener=\
    org.springframework.boot.ClearCachesApplicationListener,\
    org.springframework.boot.builder.ParentContextCloserApplicationListener,\
    org.springframework.boot.context.FileEncodingApplicationListener,\
    org.springframework.boot.context.config.AnsiOutputApplicationListener,\
    org.springframework.boot.context.config.ConfigFileApplicationListener,\
    org.springframework.boot.context.config.DelegatingApplicationListener,\
    org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
    org.springframework.boot.context.logging.LoggingApplicationListener,\
    org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
    
    # Environment Post Processors
    org.springframework.boot.env.EnvironmentPostProcessor=\
    org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
    org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
    org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor
    
    # Failure Analyzers
    org.springframework.boot.diagnostics.FailureAnalyzer=\
    org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BindValidationFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.UnboundConfigurationPropertyFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer
    
    # FailureAnalysisReporters
    org.springframework.boot.diagnostics.FailureAnalysisReporter=\
    org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55

    image-20210616075248280

    在日常工作中,我们可能需要实现一些SDK或者Spring Boot Starter给被人使用时, 我们就可以使用Factories机制。Factories机制可以让SDK或者Starter的使用只需要很少或者不需要进行配置,只需要在服务中引入我们的jar包即可。

    # ApplicationListener的应用

    springboot启动时,会触发很多监听事件(可以有顺序@order),常见的有

    ApplicationContextInitializedEventApplicationContext已加载,但bean未实例化,对应的生命周期方法是contextPrepared()

    ApplicationStartedEvent在应用程序上下文刷新之后,调用任何 ApplicationRunner 和 CommandLineRunner 运行程序之前。

    见guns开源项目:config-spring-boot-starter模块当中 cn.stylefeng.roses.kernel.config.modular.listener.ConfigInitListener

    SpringBoot-SpringBoot中的事件机制 - 知乎 (zhihu.com) (opens new window)

    参考文章:

    Spring Factories - 简书 (jianshu.com) (opens new window)

    SpringBoot 插件化开发模式详细总结 (opens new window)

    SpringBoot 原理解析
    springboot常用注解

    ← SpringBoot 原理解析 springboot常用注解→

    最近更新
    01
    国际象棋
    09-15
    02
    成语
    09-15
    03
    自然拼读
    09-15
    更多文章>
    Theme by Vdoing | Copyright © 2019-2023 zxpnet | 粤ICP备14079330号-1
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式