news 2026/6/10 13:51:28

Spring Boot spring.factories文件详细说明

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring Boot spring.factories文件详细说明

优质博文:IT-BLOG-CN

前言:经常看到 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(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/23 18:00:48

翻译大模型HY-MT1.5入门:快速上手与API调用

翻译大模型HY-MT1.5入门:快速上手与API调用 随着多语言交流需求的不断增长,高质量、低延迟的翻译模型成为AI应用中的关键组件。腾讯近期开源了混元翻译大模型1.5版本(HY-MT1.5),包含两个参数规模的模型:HY…

作者头像 李华
网站建设 2026/6/10 11:38:44

频率响应数据采集操作指南:ADC采样率设置技巧

频率响应数据采集实战:从ADC采样率设置到抗混叠滤波的全链路优化你有没有遇到过这样的情况?在做电源环路稳定性测试时,明明理论设计很稳健,Bode图却在高频段突然冒出一个诡异的“共振峰”;或者测音频放大器频率响应&am…

作者头像 李华
网站建设 2026/6/10 11:36:38

HY-MT1.5-1.8B极致优化:INT8量化后边缘设备部署教程

HY-MT1.5-1.8B极致优化:INT8量化后边缘设备部署教程 随着多语言交流需求的不断增长,高效、低延迟的翻译模型成为智能终端和边缘计算场景的关键技术。腾讯开源的混元翻译大模型HY-MT1.5系列,凭借其卓越的翻译质量与灵活的部署能力&#xff0c…

作者头像 李华
网站建设 2026/6/10 0:29:29

HY-MT1.5-1.8B部署教程:如何在边缘设备实现高效翻译

HY-MT1.5-1.8B部署教程:如何在边缘设备实现高效翻译 1. 引言 随着全球化进程的加速,高质量、低延迟的实时翻译需求日益增长。尤其是在物联网、智能终端和移动设备等边缘计算场景中,用户对离线、安全、快速响应的翻译能力提出了更高要求。腾讯…

作者头像 李华
网站建设 2026/6/10 11:35:06

翻译质量自动评估:HY-MT1.5评测系统搭建

翻译质量自动评估:HY-MT1.5评测系统搭建 随着多语言交流需求的不断增长,高质量、低延迟的机器翻译系统成为智能应用的核心组件。腾讯近期开源了混元翻译大模型1.5版本(HY-MT1.5),包含两个关键模型:HY-MT1.…

作者头像 李华
网站建设 2026/6/10 11:41:01

HY-MT1.5术语库管理:自定义词汇表使用

HY-MT1.5术语库管理:自定义词汇表使用 1. 引言 随着全球化进程的加速,高质量、可定制化的机器翻译需求日益增长。腾讯推出的混元翻译大模型 HY-MT1.5 系列,正是为应对多语言互译场景中对准确性、实时性与领域适配能力的高要求而设计。该系列…

作者头像 李华