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

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

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

前言

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

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

相信大家的項目中都會寫starter,我們團隊寫的國際化通用和通用聚合服務等即插即用的功能包,就是用的starter。那么就會自己聲明spring.factories文件。

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

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

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

spring.factories 文件是一個鍵值對的屬性文件,鍵和值之間用等號(=)分隔,多個值之間用逗號(,)分隔。文件內(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 是最常見的鍵,用于聲明自動配置類。Spring Boot 會在啟動時掃描這些類,并根據(jù)條件加載相應的配置。

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

AutoConfigurationImportListener

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

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

AutoConfigurationImportFilter

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

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

org.springframework.boot.env.PropertySourceLoader

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

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

org.springframework.boot.SpringApplicationRunListener

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

Listener for the SpringApplication run method。

初始化時機: 在SpringApplication啟動時進行初始化。

org.springframework.boot.SpringBootExceptionReporter

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

初始化時機: 在SpringApplication啟動時(創(chuàng)建完Application context后)進行初始化。

org.springframework.context.ApplicationContextInitializer

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

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

org.springframework.context.ApplicationListener

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

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

org.springframework.boot.env.EnvironmentPostProcessor

在context刷新前可對environment進行修改的組件。

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

org.springframework.boot.diagnostics.FailureAnalyzer

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

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

org.springframework.boot.diagnostics.FailureAnalysisReporter

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

初始化時機: 在SpringApplication啟動時(創(chuàng)建完Application context后)發(fā)生錯誤的情況下進行初始化。

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

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

相關(guān)文章

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

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

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

    基于springioc bean 的幾個屬性介紹

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

    JAVA實現(xiàn)的簡單萬年歷代碼

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

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

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

    java枚舉的使用示例

    我們在學習編程語言的時候都學過枚舉,現(xiàn)在就具體來看看java中的枚舉的使用
    2013-12-12
  • springboot前后臺數(shù)據(jù)交互的示例代碼

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

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

    SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題)

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

    詳解Java如何簡化條件表達式

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

    Java枚舉學習之定義和基本特性詳解

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

    解決springdataJPA對原生sql支持的問題

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

最新評論