当前位置: 代码网 > it编程>编程语言>Java > 关于SpringBoot的spring.factories文件详细说明

关于SpringBoot的spring.factories文件详细说明

2024年12月30日 Java 我要评论
前言经常看到 spring.factories 文件,却没有对它进行深入的了解和分析,今天我们就一起揭开面纱看看它的内在。spring.factories 文件是 spring boot 自动配置机制

前言

经常看到 spring.factories 文件,却没有对它进行深入的了解和分析,今天我们就一起揭开面纱看看它的内在。

spring.factories 文件是 spring boot 自动配置机制的核心部分之一。它位于每个 spring boot 自动配置模块的 meta-inf 目录下,用于声明该模块提供的自动配置类、条件性配置类、环境后处理器等。以下是对 spring.factories 文件的详细说明:

相信大家的项目中都会写starter,我们团队写的国际化通用和通用聚合服务等即插即用的功能包,就是用的starter。那么就会自己声明spring.factories文件。

这是一种工厂加载机制(factory loading mechanism),也可说成是spi机制。原理分析在spring spi与java spi、dubbo spi

spring boot jar包下的spring.factories文件也声明了一些组件,为方便我的阐述,我把它列在了附录中。我会按照 介绍作用、初始化时机 去做分析。

一、基本结构

spring.factories 文件是一个键值对的属性文件,键和值之间用等号(=)分隔,多个值之间用逗号(,)分隔。文件内容通常如下所示:

org.springframework.boot.autoconfigure.enableautoconfiguration=\
com.example.myautoconfiguration,\
com.example.anotherautoconfiguration

org.springframework.boot.autoconfigure.condition.conditionalonclass=\
com.example.someclass

org.springframework.context.applicationlistener=\
com.example.myapplicationlistener

二、常见的键

enableautoconfiguration

enableautoconfiguration 是最常见的键,用于声明自动配置类。spring boot 会在启动时扫描这些类,并根据条件加载相应的配置。

org.springframework.boot.autoconfigure.enableautoconfiguration=\
com.example.myautoconfiguration,\
com.example.anotherautoconfiguration

autoconfigurationimportlistener

autoconfigurationimportlistener 用于声明 autoconfigurationimportlistener 接口的实现类,这些类可以在自动配置类导入之前或之后执行一些逻辑。

org.springframework.boot.autoconfigure.autoconfigurationimportlistener=\
com.example.myautoconfigurationimportlistener

autoconfigurationimportfilter

autoconfigurationimportfilter 用于声明 autoconfigurationimportfilter 接口的实现类,这些类可以在自动配置类导入之前过滤掉一些不需要的配置类。

org.springframework.boot.autoconfigure.autoconfigurationimportfilter=\
com.example.myautoconfigurationimportfilter

org.springframework.boot.env.propertysourceloader

策略接口,用于加载propertysource(配置)。实现有propertiespropertysourceloader、yamlpropertysourceloader。

初始化时机: 当configfileapplicationlistener收到applicationenvironmentpreparedevent事件时, 创建sourceloader并执行load,加载配置。

org.springframework.boot.springapplicationrunlistener

用于监听spring boot启动并作出相应处理。

listener for the springapplication run method。

初始化时机: 在springapplication启动时进行初始化。

org.springframework.boot.springbootexceptionreporter

回调接口,用于对spring boot应用启动失败(发生异常)后,进行异常播报的组件。

初始化时机: 在springapplication启动时(创建完application context后)进行初始化。

org.springframework.context.applicationcontextinitializer

用于在刷新之前初始化spring configurableapplicationcontext的回调接口。比如,servlet web容器会用该组件设置一些额外的属性。

初始化时机: spring application构造时创建。

org.springframework.context.applicationlistener

用于监听事件并作处理。

初始化时机: spring application构造时创建。

org.springframework.boot.env.environmentpostprocessor

在context刷新前可对environment进行修改的组件。

初始化时机: 在run listener发出applicationenvironmentpreparedevent事件后触发。

org.springframework.boot.diagnostics.failureanalyzer

分析错误,展示给用户诊断结果。

初始化时机: 在springapplication启动时(创建完application context后)进行初始化。 伴随一个springbootexceptionreporter(即org.springframework.boot.diagnostics.failureanalyzers) 的实例化而实例化。

org.springframework.boot.diagnostics.failureanalysisreporter

在错误分析完成后,向用户展示结果。(spring boot默认实现是通过日志展示出来)

初始化时机: 在springapplication启动时(创建完application context后)发生错误的情况下进行初始化。

spring boot包的spring.factories文件

# 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.beandefinitionoverridefailureanalyzer,\
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.nosuchmethodfailureanalyzer,\
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

三、自动配置类

以下是一个简单的自动配置类示例:

package com.example.config;

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
public class myautoconfiguration {

    @bean
    public myservice myservice() {
        return new myservice();
    }
}

条件性自动配置

package com.example.config;

import org.springframework.boot.autoconfigure.condition.conditionalonclass;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
@conditionalonclass(name = "com.example.someclass")
public class myautoconfiguration {

    @bean
    public myservice myservice() {
        return new myservice();
    }
}

以上就是关于springboot的spring.factories文件详细说明的详细内容,更多关于springboot spring.factories文件的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com