1. spring.factories是什么文件?
一个Spring应用程序有一些应用监听器和应用上下文初始化器用来定制应用上下文或者环境的初始化,使用到的应用监听器和应用上下文初始化器列表就是在META-INF/spring.factories
文件中配置的。除了在spring.factories文件中配置外,还有其他两种方法可添加对上下文或者环节的定制:一种就是在SpringApplication运行之前添加自定义的监听器或初始化器;另外一种就是在properties配置文件中配置相关类。
A
SpringApplication
hasApplicationListeners
andApplicationContextInitializers
that are used to apply customizations to the context or environment. Spring Boot loads a number of such customizations for use internally fromMETA-INF/spring.factories
. There is more than one way to register additional customizations:
- Programmatically, per application, by calling the
addListeners
andaddInitializers
methods onSpringApplication
before you run it.- Declaratively, per application, by setting the
context.initializer.classes
orcontext.listener.classes
properties.- Declaratively, for all applications, by adding a
META-INF/spring.factories
and packaging a jar file that the applications all use as a library.
2. 包含spring.factories文件的jar包有哪些?
当前使用的Spring Boot版本是2.1.9RELEASE,由于不同项目使用的依赖不同,所以不同项目加载到的工厂类数据会不同,以下以一个示例项目运行时涉及的jar包为例:
(1)spring-boot-2.1.9.RELEASE.jar
(2)spring-boot-autoconfigure-2.1.9.RELEASE.jar
(3)spring-data-jpa-2.1.11.RELEASE.jar
(4)spring-data-commons-2.1.11.RELEASE.jar
(5)spring-beans-5.1.10.RELEASE.jar
(6)spring-cloud-alibaba-nacos-config-0.9.0.RELEASE.jar
(7)spring-cloud-commons-2.1.1.RELEASE.jar
(8)spring-cloud-context-2.1.1.RELEASE.jar
3. spring-boot-2.1.9.RELEASE.jar的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
...
# 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
...
4. 加载完8个jar中的spring.factories文件后得到的17类工厂类接口
(1)FailureAnalyzerorg.springframework.boot.diagnostics.FailureAnalyzer
(2)EnvironmentPostProcessororg.springframework.boot.env.EnvironmentPostProcessor
(3)SpringApplicationRunListenerorg.springframework.boot.SpringApplicationRunListener
(4)ApplicationContextInitializerorg.springframework.context.ApplicationContextInitializer
(5)PropertySourceLoaderorg.springframework.boot.env.PropertySourceLoader
(6)ApplicationListenerorg.springframework.context.ApplicationListener
(7)FailureAnalysisReporterorg.springframework.boot.diagnostics.FailureAnalysisReporter
(8)SpringBootExceptionReporterorg.springframework.boot.SpringBootExceptionReporter
(9)AutoConfigurationImportFilterorg.springframework.boot.autoconfigure.AutoConfigurationImportFilter
(10)AutoConfigurationImportListenerorg.springframework.boot.autoconfigure.AutoConfigurationImportListener
(11)TemplateAvailabilityProviderorg.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider
(12)EnableAutoConfigurationorg.springframework.boot.autoconfigure.EnableAutoConfiguration
(13)ProxyUtils$ProxyDetectororg.springframework.data.util.ProxyUtils$ProxyDetector
(14)RepositoryFactorySupportorg.springframework.data.repository.core.support.RepositoryFactorySupport
(15)SpringDataJacksonModulesorg.springframework.data.web.config.SpringDataJacksonModules
(16)BeanInfoFactoryorg.springframework.beans.BeanInfoFactory
(17)BootstrapConfigurationorg.springframework.cloud.bootstrap.BootstrapConfiguration
5. 工厂接口以及对应的实现类
org.springframework.boot.diagnostics.
(1)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, org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer, org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer, org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer, org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer, org.springframework.cloud.alibaba.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer, org.springframework.cloud.configuration.CompatibilityNotMetFailureAnalyzer]
org.springframework.boot.env.
(2)EnvironmentPostProcessor
[org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor, org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor, org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor, org.springframework.cloud.client.HostInfoEnvironmentPostProcessor]
org.springframework.boot.
(3)SpringApplicationRunListener
[org.springframework.boot.context.event.EventPublishingRunListener]
org.springframework.context.
(4)ApplicationContextInitializer
[org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer, org.springframework.boot.context.ContextIdApplicationContextInitializer, org.springframework.boot.context.config.DelegatingApplicationContextInitializer, org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer, org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer, org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener]
org.springframework.boot.env.
(5)PropertySourceLoader
[org.springframework.boot.env.PropertiesPropertySourceLoader, org.springframework.boot.env.YamlPropertySourceLoader]
org.springframework.context.
(6)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
org.springframework.boot.autoconfigure.BackgroundPreinitializer
org.springframework.cloud.bootstrap.BootstrapApplicationListener
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,
org.springframework.cloud.context.restart.RestartListener
org.springframework.boot.diagnostics.
(7)FailureAnalysisReporter
[org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter]
org.springframework.boot.
(8)SpringBootExceptionReporter
[org.springframework.boot.diagnostics.FailureAnalyzers]
org.springframework.boot.autoconfigure.
(9)AutoConfigurationImportFilter
[org.springframework.boot.autoconfigure.condition.OnBeanCondition, org.springframework.boot.autoconfigure.condition.OnClassCondition, org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition]
org.springframework.boot.autoconfigure.
(10)AutoConfigurationImportListener
[org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener]
org.springframework.boot.autoconfigure.template.
(11)TemplateAvailabilityProvider
[org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider, org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider, org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider, org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider, org.springframework.boot.autoconfigure.web.servlet.JspTemplateAvailabilityProvider]
org.springframework.boot.autoconfigure.
(12)EnableAutoConfiguration
[org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration, org.springframework.boot.autoconfigure.aop.AopAutoConfiguration, org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration, org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration, org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration, org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration, org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration, org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration, org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,
... //太多了中间部分省略了
org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration,
org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration, org.springframework.cloud.client.CommonsClientAutoConfiguration, org.s...
org.springframework.data.util.
(13)ProxyUtils$ProxyDetector
[org.springframework.data.jpa.util.HibernateProxyDetector]
org.springframework.data.repository.core.support.
(14)RepositoryFactorySupport
[org.springframework.data.jpa.repository.support.JpaRepositoryFactory]
org.springframework.data.web.config.
(15)SpringDataJacksonModules
[org.springframework.data.web.config.SpringDataJacksonConfiguration]
org.springframework.beans.
(16)BeanInfoFactory
[org.springframework.beans.ExtendedBeanInfoFactory]
org.springframework.cloud.bootstrap.
(17)BootstrapConfiguration
[org.springframework.cloud.alibaba.nacos.NacosConfigBootstrapConfiguration, org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration, org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration, org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration, org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration]
网友评论