avatar

目录
spring.factories自动化配置归类

前言

为什么要有这篇文章?

当我们分析在spring boot源码时,经常会看到SpringFactoriesLoader.loadFactoryNames(xxx.classs)返回了很多自动化配置类的名称,比如EnableAutoConfiguration=xxxx, 虽然可以看到EnableAutoConfiguration具体加载的配置有哪些,但是它的值到底是从哪个项目来的,自动化配置了哪个项目可能还是很模糊,找对应的配置可能还需要花费一定的时间。

自动化配置的地方

BootstrapConfiguration

  • spring-cloud-context-2.2.0.RELEASE.jar!\META-INF\spring.factories 共4个

    properties
    1
    2
    3
    4
    5
    6
    7

    # Bootstrap components
    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\
    org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\
    org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
  • spring-cloud-netflix-eureka-client-2.2.0.RELEASE.jar!\META-INF\spring.factories共1个

    properties
    1
    2
    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    org.springframework.cloud.netflix.eureka.config.EurekaDiscoveryClientConfigServiceBootstrapConfiguration

ApplicationListener

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories 共9个
Code
1
2
3
4
5
6
7
8
9
10
11
## 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
  • spring-cloud-context-2.2.0.RELEASE.jar!\META-INF\spring.factories 共3个
Code
1
2
3
4
5
## Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.bootstrap.BootstrapApplicationListener,\
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,\
org.springframework.cloud.context.restart.RestartListener
  • spring-boot-autoconfigure-2.2.1.RELEASE.jar!\META-INF\spring.factories 共1个
Code
1
2
3
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

ApplicationListener自动化配置加载原理

我们在编写一个spring boot应用时通常启动的方式是通过SpringApplication.run(xxx.class, args)来启动的,

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class SpringApplication {

public SpringApplication(Class... primarySources) {
this(null, primarySources);
}

public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 关键代码:加载spring.factories中key为ApplicationContextInitializer的自动化配置类的名称
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 关键代码:加载spring.factories中key为ApplicationListener的自动化配置类的名称
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}

public static ConfigurableApplicationContext run(Class primarySource, String... args) {
return run(new Class[] { primarySource }, args);
}

public static ConfigurableApplicationContext run(Class[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
}

ApplicationContextInitializer

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    5
    6
    7
    # 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.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
    org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
  • spring-boot-autoconfigure-2.2.1.RELEASE.jar!\META-INF\spring.factories 共2个

    Code
    1
    2
    3
    4
    # Initializers
    org.springframework.context.ApplicationContextInitializer=\
    org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
    org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

加载ApplicationContextInitializer的源码见上一节ApplicationListener自动化配置加载原理

EnableAutoConfiguration

  • spring-boot-autoconfigure-2.2.1.RELEASE.jar!\META-INF\spring.factories 共2个
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Auto Configure
org.springframework.boot.autoconfigure.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.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
...省略
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
  • spring-cloud-commons-2.2.0.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # AutoConfiguration
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.cloud.client.CommonsClientAutoConfiguration,\
    org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration,\
    org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration,\
    org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration,\
    org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration,\
    org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration,\
    org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration,\
    org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfiguration,\
    org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration,\
    org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration,\
    org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration,\
    org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerClientAutoConfiguration,\
    org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancerAutoConfiguration,\
    org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,\
    org.springframework.cloud.commons.httpclient.HttpClientConfiguration,\
    org.springframework.cloud.commons.util.UtilAutoConfiguration,\
    org.springframework.cloud.configuration.CompatibilityVerifierAutoConfiguration,\
    org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration
  • spring-cloud-context-2.2.0.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    5
    6
    7
    # AutoConfiguration
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
    org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration,\
    org.springframework.cloud.autoconfigure.RefreshAutoConfiguration,\
    org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration,\
    org.springframework.cloud.autoconfigure.WritableEnvironmentEndpointAutoConfiguration
  • spring-cloud-loadbalancer-2.2.0.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    5
    # AutoConfiguration
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration,\
    org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration,\
    org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration
  • spring-cloud-openfeign-core-2.2.0.RELEASE.jar!\META-INF\spring.factories
Code
1
2
3
4
5
6
7
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration,\
org.springframework.cloud.openfeign.hateoas.FeignHalAutoConfiguration,\
org.springframework.cloud.openfeign.FeignAutoConfiguration,\
org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration,\
org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration,\
org.springframework.cloud.openfeign.loadbalancer.FeignLoadBalancerAutoConfiguration
  • spring-cloud-netflix-ribbon-2.2.0.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration
  • spring-cloud-netflix-hystrix-2.2.0.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\
    org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerAutoConfiguration,\
    org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration

EnableAutoConfiguration自动化配置加载原理

@SpringBootApplication注解上可以看到有@EnableAutoConfiguration注解,

SpringBootApplication类的源码如下:

java
1
2
3
4
5
6
7
8
9
10
11
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
...省略代码
}
Code
1
2
3
4
5
6
7
8
9
10
11

```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

}

从上面可以看到它引入了AutoConfigurationImportSelector这个类,该类的具体实现见springboot2-2自动注入文件spring-factories如何加载详解

EnvironmentPostProcessor

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories
Code
1
2
3
4
5
6
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor,\
org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor
  • spring-cloud-commons-2.2.0.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    # Environment Post Processors
    org.springframework.boot.env.EnvironmentPostProcessor=\
    org.springframework.cloud.client.HostInfoEnvironmentPostProcessor

PropertySourceLoader

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    # PropertySource Loaders
    org.springframework.boot.env.PropertySourceLoader=\
    org.springframework.boot.env.PropertiesPropertySourceLoader,\
    org.springframework.boot.env.YamlPropertySourceLoader

SpringApplicationRunListener

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4

    # Run Listeners
    org.springframework.boot.SpringApplicationRunListener=\
    org.springframework.boot.context.event.EventPublishingRunListener

EnableCircuitBreaker

  • spring-cloud-netflix-hystrix-2.2.0.RELEASE.jar!\META-INF\spring.factories
Code
1
2
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration

BeanInfoFactory

  • spring-beans-5.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    org.springframework.beans.BeanInfoFactory=org.springframework.beans.ExtendedBeanInfoFactory

AutoConfigurationImportListener

  • spring-boot-autoconfigure-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    # Auto Configuration Import Listeners
    org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
    org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

AutoConfigurationImportFilter

  • spring-boot-autoconfigure-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    5
    # Auto Configuration Import Filters
    org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
    org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
    org.springframework.boot.autoconfigure.condition.OnClassCondition,\
    org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition

SpringBootExceptionReporter

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    # Error Reporters
    org.springframework.boot.SpringBootExceptionReporter=\
    org.springframework.boot.diagnostics.FailureAnalyzers

TemplateAvailabilityProvider

  • spring-boot-autoconfigure-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    4
    5
    6
    7
    # Template availability providers
    org.springframework.boot.autoconfigure.template.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

FailureAnalysisReporter

  • spring-boot-2.2.1.RELEASE.jar!\META-INF\spring.factories

    Code
    1
    2
    3
    # FailureAnalysisReporters
    org.springframework.boot.diagnostics.FailureAnalysisReporter=\
    org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
文章作者: calebzhao
文章链接: https://calebzhao.github.io/2019/12/29/spring.factories%E8%87%AA%E5%8A%A8%E5%8C%96%E9%85%8D%E7%BD%AE%E5%BD%92%E7%B1%BB/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 calebzhao的博客
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论