欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于SpringBoot的spring.factories文件詳細(xì)說明

 更新時(shí)間:2024年12月29日 08:50:39   作者:程序猿進(jìn)階  
spring.factories 文件是 Spring Boot 自動(dòng)配置機(jī)制的核心部分之一,它位于每個(gè) Spring Boot 自動(dòng)配置模塊的 META-INF 目錄下,經(jīng)常看到 spring.factories 文件,卻沒有對(duì)它進(jìn)行深入的了解和分析,今天我們就一起揭開面紗看看它的內(nèi)在,需要的朋友可以參考下

前言

經(jīng)??吹?spring.factories 文件,卻沒有對(duì)它進(jìn)行深入的了解和分析,今天我們就一起揭開面紗看看它的內(nèi)在。

spring.factories 文件是 Spring Boot 自動(dòng)配置機(jī)制的核心部分之一。它位于每個(gè) Spring Boot 自動(dòng)配置模塊的 META-INF 目錄下,用于聲明該模塊提供的自動(dòng)配置類、條件性配置類、環(huán)境后處理器等。以下是對(duì) spring.factories 文件的詳細(xì)說明:

相信大家的項(xiàng)目中都會(huì)寫starter,我們團(tuán)隊(duì)寫的國(guó)際化通用和通用聚合服務(wù)等即插即用的功能包,就是用的starter。那么就會(huì)自己聲明spring.factories文件。

這是一種工廠加載機(jī)制(factory loading mechanism),也可說成是SPI機(jī)制。原理分析在Spring SPI與Java SPI、Dubbo SPI

Spring Boot jar包下的spring.factories文件也聲明了一些組件,為方便我的闡述,我把它列在了附錄中。我會(huì)按照 介紹作用、初始化時(shí)機(jī) 去做分析。

一、基本結(jié)構(gòu)

spring.factories 文件是一個(gè)鍵值對(duì)的屬性文件,鍵和值之間用等號(hào)(=)分隔,多個(gè)值之間用逗號(hào)(,)分隔。文件內(nèi)容通常如下所示:

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 是最常見的鍵,用于聲明自動(dòng)配置類。Spring Boot 會(huì)在啟動(dòng)時(shí)掃描這些類,并根據(jù)條件加載相應(yīng)的配置。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.MyAutoConfiguration,\
com.example.AnotherAutoConfiguration

AutoConfigurationImportListener

AutoConfigurationImportListener 用于聲明 AutoConfigurationImportListener 接口的實(shí)現(xiàn)類,這些類可以在自動(dòng)配置類導(dǎo)入之前或之后執(zhí)行一些邏輯。

org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
com.example.MyAutoConfigurationImportListener

AutoConfigurationImportFilter

AutoConfigurationImportFilter 用于聲明 AutoConfigurationImportFilter 接口的實(shí)現(xiàn)類,這些類可以在自動(dòng)配置類導(dǎo)入之前過濾掉一些不需要的配置類。

org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
com.example.MyAutoConfigurationImportFilter

org.springframework.boot.env.PropertySourceLoader

策略接口,用于加載PropertySource(配置)。實(shí)現(xiàn)有PropertiesPropertySourceLoader、YamlPropertySourceLoader。

初始化時(shí)機(jī): 當(dāng)ConfigFileApplicationListener收到ApplicationEnvironmentPreparedEvent事件時(shí), 創(chuàng)建SourceLoader并執(zhí)行l(wèi)oad,加載配置。

org.springframework.boot.SpringApplicationRunListener

用于監(jiān)聽spring boot啟動(dòng)并作出相應(yīng)處理。

Listener for the SpringApplication run method。

初始化時(shí)機(jī): 在SpringApplication啟動(dòng)時(shí)進(jìn)行初始化。

org.springframework.boot.SpringBootExceptionReporter

回調(diào)接口,用于對(duì)Spring Boot應(yīng)用啟動(dòng)失?。òl(fā)生異常)后,進(jìn)行異常播報(bào)的組件。

初始化時(shí)機(jī): 在SpringApplication啟動(dòng)時(shí)(創(chuàng)建完Application context后)進(jìn)行初始化。

org.springframework.context.ApplicationContextInitializer

用于在刷新之前初始化Spring ConfigurableApplicationContext的回調(diào)接口。比如,servlet web容器會(huì)用該組件設(shè)置一些額外的屬性。

初始化時(shí)機(jī): Spring Application構(gòu)造時(shí)創(chuàng)建。

org.springframework.context.ApplicationListener

用于監(jiān)聽事件并作處理。

初始化時(shí)機(jī): Spring Application構(gòu)造時(shí)創(chuàng)建。

org.springframework.boot.env.EnvironmentPostProcessor

在context刷新前可對(duì)environment進(jìn)行修改的組件。

初始化時(shí)機(jī): 在run listener發(fā)出ApplicationEnvironmentPreparedEvent事件后觸發(fā)。

org.springframework.boot.diagnostics.FailureAnalyzer

分析錯(cuò)誤,展示給用戶診斷結(jié)果。

初始化時(shí)機(jī): 在SpringApplication啟動(dòng)時(shí)(創(chuàng)建完Application context后)進(jìn)行初始化。 伴隨一個(gè)SpringBootExceptionReporter(即org.springframework.boot.diagnostics.FailureAnalyzers) 的實(shí)例化而實(shí)例化。

org.springframework.boot.diagnostics.FailureAnalysisReporter

在錯(cuò)誤分析完成后,向用戶展示結(jié)果。(Spring Boot默認(rèn)實(shí)現(xiàn)是通過日志展示出來)

初始化時(shí)機(jī): 在SpringApplication啟動(dòng)時(shí)(創(chuàng)建完Application context后)發(fā)生錯(cuò)誤的情況下進(jìn)行初始化。

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

三、自動(dòng)配置類

以下是一個(gè)簡(jiǎn)單的自動(dòng)配置類示例:

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();
    }
}

條件性自動(dòng)配置

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();
    }
}

以上就是關(guān)于SpringBoot的spring.factories文件詳細(xì)說明的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot spring.factories文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 帶你3分鐘帶你搞定Spring Boot中Schedule

    帶你3分鐘帶你搞定Spring Boot中Schedule

    本文主要圍繞Spring scheduled應(yīng)用實(shí)踐進(jìn)行分享,如果是單體應(yīng)用,使用SpringBoot內(nèi)置的@scheduled注解可以解決大部分業(yè)務(wù)需求,對(duì)Spring Boot中Schedule 相關(guān)知識(shí)感興趣的朋友一起看看吧
    2024-07-07
  • 基于springioc bean 的幾個(gè)屬性介紹

    基于springioc bean 的幾個(gè)屬性介紹

    下面小編就為大家?guī)硪黄趕pringioc bean 的幾個(gè)屬性介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼

    JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼

    這篇文章主要介紹了JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼,涉及Java日期操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • sharding-jdbc5.0.0實(shí)現(xiàn)分表實(shí)踐

    sharding-jdbc5.0.0實(shí)現(xiàn)分表實(shí)踐

    本文主要介紹了sharding-jdbc5.0.0分表實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • java枚舉的使用示例

    java枚舉的使用示例

    我們?cè)趯W(xué)習(xí)編程語(yǔ)言的時(shí)候都學(xué)過枚舉,現(xiàn)在就具體來看看java中的枚舉的使用
    2013-12-12
  • springboot前后臺(tái)數(shù)據(jù)交互的示例代碼

    springboot前后臺(tái)數(shù)據(jù)交互的示例代碼

    這篇文章主要介紹了springboot前后臺(tái)數(shù)據(jù)交互的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • SpringCloud啟動(dòng)eureka server后,沒報(bào)錯(cuò)卻不能訪問管理頁(yè)面(404問題)

    SpringCloud啟動(dòng)eureka server后,沒報(bào)錯(cuò)卻不能訪問管理頁(yè)面(404問題)

    這篇文章主要介紹了SpringCloud啟動(dòng)eureka server后,沒報(bào)錯(cuò)卻不能訪問管理頁(yè)面(404問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解Java如何簡(jiǎn)化條件表達(dá)式

    詳解Java如何簡(jiǎn)化條件表達(dá)式

    在復(fù)雜的實(shí)際業(yè)務(wù)中,往往會(huì)出現(xiàn)各種嵌套的條件判斷邏輯。隨著需求的增加,條件邏輯會(huì)變得越來越復(fù)雜。面對(duì)這種情況,簡(jiǎn)化判斷邏輯就是不得不做的事情,下面為大家介紹幾種方法
    2022-06-06
  • Java枚舉學(xué)習(xí)之定義和基本特性詳解

    Java枚舉學(xué)習(xí)之定義和基本特性詳解

    枚舉是JAVA?5.0后增加的一個(gè)重要類型??梢杂脕肀硎疽唤M取值范圍固定的變量。本文將通過示例為大家詳細(xì)講解枚舉的定義和基本特性,感興趣的可以了解一下
    2022-08-08
  • 解決springdataJPA對(duì)原生sql支持的問題

    解決springdataJPA對(duì)原生sql支持的問題

    這篇文章主要介紹了解決springdataJPA對(duì)原生sql支持的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論