“Spring Boot 自动包规则原理”的版本间的差异
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的23个中间版本) | |||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV19K4y1L7MT/?p=13 | https://www.bilibili.com/video/BV19K4y1L7MT/?p=13 | ||
− | === | + | === 引导加载的自动配置类 === |
− | ==== | + | ==== @SpringBootApplication ==== |
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
…… | …… | ||
第13行: | 第13行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | ===== | + | ===== @SpringBootConfiguration ===== |
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
@Configuration | @Configuration | ||
第20行: | 第20行: | ||
====== @Configuration ====== | ====== @Configuration ====== | ||
− | + | 代表当前是一个配置类,使用 @SpringBootApplication 的类也会是 Spring Boot 中的一个配置类 | |
− | ==== | + | ==== @ComponentScan ==== |
+ | |||
+ | 包扫描注解,指定扫描哪些包; | ||
+ | |||
− | |||
==== @EnableAutoConfiguration ==== | ==== @EnableAutoConfiguration ==== | ||
第39行: | 第41行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | ==== @AutoConfigurationPackage ==== | + | ===== @AutoConfigurationPackage ===== |
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
@Retention(RetentionPolicy.RUNTIME) | @Retention(RetentionPolicy.RUNTIME) | ||
第46行: | 第48行: | ||
@Import(AutoConfigurationPackages.Registrar.class) //给容器中导入一个组件 | @Import(AutoConfigurationPackages.Registrar.class) //给容器中导入一个组件 | ||
public @interface AutoConfigurationPackage { | public @interface AutoConfigurationPackage { | ||
+ | </syntaxhighlight>比较关键的是 <code>@Import(AutoConfigurationPackages.Registrar.class)</code> 中导入的 AutoConfigurationPackages.Registrar | ||
+ | |||
+ | ====== AutoConfigurationPackages.java ====== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | …… | ||
+ | static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports { | ||
+ | |||
+ | @Override | ||
+ | public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { | ||
+ | register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0])); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public Set<Object> determineImports(AnnotationMetadata metadata) { | ||
+ | return Collections.singleton(new PackageImports(metadata)); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | …… | ||
+ | </syntaxhighlight>利用 Registrar 向容器中导入一系列组件,<code>AnnotationMetadata metadata</code> 传入的是 @SpringBootApplication 注解标注类的注解元信息, | ||
+ | |||
+ | 然后经由 <code>new PackageImport(metadata).getPackageName()</code> 得到 @SpringBootApplication 注解标注类的包名(比如:io.github.jihch), | ||
+ | |||
+ | 最终目的是将指定一个包下(@SpringBootApplication 注解标注类的包)的所有组件导入进来, | ||
+ | |||
+ | 这就解释了为什么默认是导入 @SpringBootApplication 注解标注类的包下的所有组件 | ||
+ | |||
+ | |||
+ | |||
+ | == 初始化加载自动配置类 == | ||
+ | https://www.bilibili.com/video/BV19K4y1L7MT?p=14 | ||
+ | |||
+ | ===== @Import(AutoConfigurationImportSelector.class) ===== | ||
+ | |||
+ | ====== AutoConfigurationImportSelector.java ====== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | …… | ||
+ | @Override | ||
+ | public String[] selectImports(AnnotationMetadata annotationMetadata) { | ||
+ | if (!isEnabled(annotationMetadata)) { | ||
+ | return NO_IMPORTS; | ||
+ | } | ||
+ | AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata); | ||
+ | return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); | ||
+ | } | ||
+ | …… | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ====== 利用 <code>getAutoConfigurationEntry(annotationMetadata)</code> 往容器中批量导入一些组件: ====== | ||
+ | [[文件:利用 getAutoConfigurationEntry 导入组件.png|无|缩略图|1151x1151像素]]<syntaxhighlight lang="java"> | ||
+ | …… | ||
+ | protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) { | ||
+ | if (!isEnabled(annotationMetadata)) { | ||
+ | return EMPTY_ENTRY; | ||
+ | } | ||
+ | AnnotationAttributes attributes = getAttributes(annotationMetadata); | ||
+ | List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); | ||
+ | configurations = removeDuplicates(configurations); | ||
+ | Set<String> exclusions = getExclusions(annotationMetadata, attributes); | ||
+ | checkExcludedClasses(configurations, exclusions); | ||
+ | configurations.removeAll(exclusions); | ||
+ | configurations = getConfigurationClassFilter().filter(configurations); | ||
+ | fireAutoConfigurationImportEvents(configurations, exclusions); | ||
+ | return new AutoConfigurationEntry(configurations, exclusions); | ||
+ | } | ||
+ | …… | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | ====== 用 <code>List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);</code> 获取到所有需要导入到容器中的组件: ====== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | …… | ||
+ | protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { | ||
+ | List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), | ||
+ | getBeanClassLoader()); | ||
+ | Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " | ||
+ | + "are using a custom packaging, make sure that file is correct."); | ||
+ | return configurations; | ||
+ | } | ||
+ | …… | ||
+ | </syntaxhighlight>SpringFactoriesLoader.java<syntaxhighlight lang="java"> | ||
+ | public final class SpringFactoriesLoader { | ||
+ | …… | ||
+ | public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"; | ||
+ | …… | ||
+ | public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) { | ||
+ | String factoryTypeName = factoryType.getName(); | ||
+ | return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList()); | ||
+ | } | ||
+ | private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { | ||
+ | MultiValueMap<String, String> result = cache.get(classLoader); | ||
+ | if (result != null) { | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | Enumeration<URL> urls = (classLoader != null ? | ||
+ | classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : | ||
+ | ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); | ||
+ | result = new LinkedMultiValueMap<>(); | ||
+ | while (urls.hasMoreElements()) { | ||
+ | URL url = urls.nextElement(); | ||
+ | UrlResource resource = new UrlResource(url); | ||
+ | Properties properties = PropertiesLoaderUtils.loadProperties(resource); | ||
+ | for (Map.Entry<?, ?> entry : properties.entrySet()) { | ||
+ | String factoryTypeName = ((String) entry.getKey()).trim(); | ||
+ | for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { | ||
+ | result.add(factoryTypeName, factoryImplementationName.trim()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | cache.put(classLoader, result); | ||
+ | return result; | ||
+ | } | ||
+ | catch (IOException ex) { | ||
+ | throw new IllegalArgumentException("Unable to load factories from location [" + | ||
+ | FACTORIES_RESOURCE_LOCATION + "]", ex); | ||
+ | } | ||
+ | } | ||
+ | …… | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ====== 利用工厂加载 <code>Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader)</code> 得到所有的组件; ====== | ||
+ | |||
+ | ====== 从 META-INF/spring.factories 位置来加载一个文件。 ====== | ||
+ | |||
+ | ====== 默认扫描当前系统里面所有 META-INF/spring.factories 位置的文件 ====== | ||
+ | |||
+ | ====== 比如:spring-boot-2.3.4.RELEASE.jar/META-INF/spring.factories ====== | ||
+ | <syntaxhighlight lang="properties"> | ||
+ | # PropertySource Loaders | ||
+ | org.springframework.boot.env.PropertySourceLoader=\ | ||
+ | org.springframework.boot.env.PropertiesPropertySourceLoader,\ | ||
+ | org.springframework.boot.env.YamlPropertySourceLoader | ||
+ | |||
+ | …… | ||
+ | |||
+ | # FailureAnalysisReporters | ||
+ | org.springframework.boot.diagnostics.FailureAnalysisReporter=\ | ||
+ | org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | ====== 最核心的是:spring-boot-autoconfigure-2.3.4.RELEASE.jar/META-INF/spring.factories ====== | ||
+ | <syntaxhighlight lang="properties"> | ||
+ | # Initializers | ||
+ | org.springframework.context.ApplicationContextInitializer=\ | ||
+ | org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\ | ||
+ | org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener | ||
+ | |||
+ | # Application Listeners | ||
+ | org.springframework.context.ApplicationListener=\ | ||
+ | org.springframework.boot.autoconfigure.BackgroundPreinitializer | ||
+ | |||
+ | # Auto Configuration Import Listeners | ||
+ | org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\ | ||
+ | org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener | ||
+ | |||
+ | # 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 | ||
+ | |||
+ | # 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.cassandra.CassandraAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\ | ||
+ | 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.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\ | ||
+ | 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.ReactiveElasticsearchRestClientAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\ | ||
+ | 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.data.rest.RepositoryRestMvcAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\ | ||
+ | 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.jms.activemq.ActiveMQAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\ | ||
+ | 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.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\ | ||
+ | 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,\ | ||
+ | org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\ | ||
+ | org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration | ||
+ | |||
+ | # Failure analyzers | ||
+ | org.springframework.boot.diagnostics.FailureAnalyzer=\ | ||
+ | org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\ | ||
+ | org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\ | ||
+ | org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\ | ||
+ | org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\ | ||
+ | org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\ | ||
+ | org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\ | ||
+ | org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer | ||
+ | |||
+ | # 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 | ||
+ | |||
+ | </syntaxhighlight># Auto Configure 开始的除<code>org.springframework.boot.autoconfigure.EnableAutoConfiguration</code>以外的就是<code>getAutoConfigurationEntry(annotationMetadata)</code> 返回的127个组件 | ||
+ | |||
+ | 文件里面写死了 Spring Boot 一启动就要给容器中加载的所有配置类 | ||
+ | |||
+ | 虽然127个场景的所有自动配置启动的时候默认全部加载 | ||
+ | |||
+ | 但最终会按需配置,比如:<syntaxhighlight lang="java"> | ||
+ | package org.springframework.boot.autoconfigure.jdbc; | ||
+ | |||
+ | …… | ||
+ | |||
+ | @Configuration(proxyBeanMethods = false) | ||
+ | @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) | ||
+ | @ConditionalOnMissingBean(type = "io.r2dbc.spi.ConnectionFactory") | ||
+ | @EnableConfigurationProperties(DataSourceProperties.class) | ||
+ | @Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class }) | ||
+ | public class DataSourceAutoConfiguration { | ||
+ | |||
+ | @Configuration(proxyBeanMethods = false) | ||
+ | @Conditional(EmbeddedDatabaseCondition.class) | ||
+ | @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) | ||
+ | @Import(EmbeddedDataSourceConfiguration.class) | ||
+ | protected static class EmbeddedDatabaseConfiguration { | ||
+ | |||
+ | } | ||
+ | |||
+ | …… | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight>都是有 <code>@Conditional</code> 注解,按照条件装备规则,最终会按需配置。 | ||
+ | |||
+ | @12:00 |
2023年3月5日 (日) 09:38的最新版本
https://www.bilibili.com/video/BV19K4y1L7MT/?p=13
引导加载的自动配置类
@SpringBootApplication
……
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@SpringBootConfiguration
@Configuration
public @interface SpringBootConfiguration {
@Configuration
代表当前是一个配置类,使用 @SpringBootApplication 的类也会是 Spring Boot 中的一个配置类
@ComponentScan
包扫描注解,指定扫描哪些包;
@EnableAutoConfiguration
……
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
……
@AutoConfigurationPackage
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class) //给容器中导入一个组件
public @interface AutoConfigurationPackage {
比较关键的是 @Import(AutoConfigurationPackages.Registrar.class)
中导入的 AutoConfigurationPackages.Registrar
AutoConfigurationPackages.java
……
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));
}
@Override
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new PackageImports(metadata));
}
}
……
利用 Registrar 向容器中导入一系列组件,AnnotationMetadata metadata
传入的是 @SpringBootApplication 注解标注类的注解元信息,
然后经由 new PackageImport(metadata).getPackageName()
得到 @SpringBootApplication 注解标注类的包名(比如:io.github.jihch),
最终目的是将指定一个包下(@SpringBootApplication 注解标注类的包)的所有组件导入进来,
这就解释了为什么默认是导入 @SpringBootApplication 注解标注类的包下的所有组件
初始化加载自动配置类
https://www.bilibili.com/video/BV19K4y1L7MT?p=14
@Import(AutoConfigurationImportSelector.class)
AutoConfigurationImportSelector.java
……
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
……
利用 getAutoConfigurationEntry(annotationMetadata)
往容器中批量导入一些组件:
……
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
……
用 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
获取到所有需要导入到容器中的组件:
……
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
……
SpringFactoriesLoader.java
public final class SpringFactoriesLoader {
……
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
……
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
result.add(factoryTypeName, factoryImplementationName.trim());
}
}
}
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}
……
}
利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader)
得到所有的组件;
从 META-INF/spring.factories 位置来加载一个文件。
默认扫描当前系统里面所有 META-INF/spring.factories 位置的文件
比如:spring-boot-2.3.4.RELEASE.jar/META-INF/spring.factories
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
……
# FailureAnalysisReporters
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
最核心的是:spring-boot-autoconfigure-2.3.4.RELEASE.jar/META-INF/spring.factories
# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer
# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
# 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
# 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.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
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.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
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.ReactiveElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\
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.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
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.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\
org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
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.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
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,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration
# Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\
org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\
org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer
# 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
# Auto Configure 开始的除org.springframework.boot.autoconfigure.EnableAutoConfiguration
以外的就是getAutoConfigurationEntry(annotationMetadata)
返回的127个组件
文件里面写死了 Spring Boot 一启动就要给容器中加载的所有配置类
虽然127个场景的所有自动配置启动的时候默认全部加载
但最终会按需配置,比如:
package org.springframework.boot.autoconfigure.jdbc;
……
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@ConditionalOnMissingBean(type = "io.r2dbc.spi.ConnectionFactory")
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class })
public class DataSourceAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@Conditional(EmbeddedDatabaseCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import(EmbeddedDataSourceConfiguration.class)
protected static class EmbeddedDatabaseConfiguration {
}
……
}
都是有 @Conditional
注解,按照条件装备规则,最终会按需配置。
@12:00