当前位置: 首页 > news >正文

SpringBoot面试之SpringBoot自动装配原理

SpringBoot自动装配原理

背景

最近因为各种原因,我又重新加入到了找工作的大军当中。昨天在面试的时候与面试官聊到我们项目都是基于SpringBoot开发的,然后面试官就顺口问了句:”SpringBoot项目会引入许多的starter,比如:mybatis-plus-boot-starter等,它是怎么完成加载的?又是怎么完成按需加载的呢?“。当时听到这个问题有点懵,稳了一秒后反应过来,这不就是在问SpringBoot自动装配的原理吗?

引出问题

什么是SpringBoot自动装配

SpringBoot 定义了一套接口规范,这套规范规定:SpringBoot 在启动时会扫描外部引用 jar 包中的META-INF/spring.factories文件,将文件中配置的类型信息加载到 Spring 容器,并执行类中定义的各种操作。对于外部 jar 来说,只需要按照 SpringBoot 定义的标准,就能将自己的功能装置进 SpringBoot。

简单来说,SpringBoot在启动时会扫描META-INF/spring.factories文件,把配置文件中的的相关配置类信息加载到Spring容器中,交给容器管理。

SpringBoot自动装配的原理

核心注解@SpringBootApplication
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}), @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})})public @interface SpringBootApplication {@AliasFor(annotation = EnableAutoConfiguration.class)Class<?>[] exclude() default {};@AliasFor(annotation = EnableAutoConfiguration.class)String[] excludeName() default {};@AliasFor(annotation = ComponentScan.class,attribute = "basePackages")String[] scanBasePackages() default {};@AliasFor(annotation = ComponentScan.class,attribute = "basePackageClasses")Class<?>[] scanBasePackageClasses() default {};@AliasFor(annotation = ComponentScan.class,attribute = "nameGenerator")Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;@AliasFor(annotation = Configuration.class)boolean proxyBeanMethods() default true;}

我们可以看到@SpringBootApplication注解下面包含了三个注解:

@SpringBootConfiguration:声明是启动类一个配置类

@EnableAutoConfiguration:启用SpringBoot的自动装配机制

@ComponentScan:扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描启动类所在的包下所有的类 ,可以自定义不扫描某些 bean。如下图所示,容器中将排除TypeExcludeFilterAutoConfigurationExcludeFilter

@EnableAutoConfiguration注解是实现自动装配的核心注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
// 将main包下所有的组件注册Spring容器中
@AutoConfigurationPackage
// 加载自动装配的类
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";Class<?>[] exclude() default {};String[] excludeName() default {};
}

@Import注解注入了一个类AutoConfigurationImportSelector.class,实现自动装配的核心代码就在AutoConfigurationImportSelector这个文件中。

AutoConfigurationImportSelector加载自动装配的类

AutoConfigurationImportSelector类的源码

public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {public AutoConfigurationImportSelector() {}public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!this.isEnabled(annotationMetadata)) {return NO_IMPORTS;} else {AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());}}
}

AutoConfigurationImportSelector是实现了DeferredImportSelector接口,我们看下DeferredImportSelector接口源码

public interface DeferredImportSelector extends ImportSelector {@Nullabledefault Class<? extends DeferredImportSelector.Group> getImportGroup() {return null;}public interface Group {void process(AnnotationMetadata metadata, DeferredImportSelector selector);Iterable<DeferredImportSelector.Group.Entry> selectImports();public static class Entry {private final AnnotationMetadata metadata;private final String importClassName;public Entry(AnnotationMetadata metadata, String importClassName) {this.metadata = metadata;this.importClassName = importClassName;}.........}}
}

DeferredImportSelector接口又继承了ImportSelector类,里面有一个核心的方法selectImports,获取自动注入类的集合。

public interface ImportSelector {String[] selectImports(AnnotationMetadata importingClassMetadata);@Nullabledefault Predicate<String> getExclusionFilter() {return null;}
}

从源码可以看出,ImportSelector类中的selectImports方法,是获取所有的需要被注册的bean的类的全名的数组集合。

接下来我们分析一下AutoConfigurationImportSelector类的ImportSelector方法

public String[] selectImports(AnnotationMetadata annotationMetadata) {// 是否开启自动装配if (!this.isEnabled(annotationMetadata)) {return NO_IMPORTS;} else {// 获取所有需要装配的beanAutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());}}

getAutoConfigurationEntry方法我们需要重点关注:

protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {// 再次判断自动装配是否开启if (!this.isEnabled(annotationMetadata)) {return EMPTY_ENTRY;} else {// 获取 @EnableAutoConfiguration注解的 exclude 和 excludeName 属性AnnotationAttributes attributes = this.getAttributes(annotationMetadata);// 获取该依赖下的 spring.factories配置文件的内容,所有的自动装配的类名List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);// 删除重复的数据,去重的作用configurations = this.removeDuplicates(configurations);// 获取spring.autoconfigure.exclude 配置文件的内容并与exclude 和 excludeName 属性合并到一个集合中Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);// 检验configurations中需要排除的内容this.checkExcludedClasses(configurations, exclusions);// 移除configurations中需要排除的内容configurations.removeAll(exclusions);// 再次对configurations中的内容进行过滤configurations = this.getConfigurationClassFilter().filter(configurations);// 关闭spring监听器中的自动装配事件this.fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);}
}

这里我们重点关注一下getCandidateConfigurations方法

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.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#loadFactoryNames方法传入EnableAutoConfiguration类和ClassLoader类加载器

    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {ClassLoader classLoaderToUse = classLoader;if (classLoader == null) {classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();}String factoryTypeName = factoryType.getName();return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());}

loadSpringFactories方法里是吧spring.factories里的相关配置类加载到容器中

private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {Map<String, List<String>> result = cache.get(classLoader);if (result != null) {return result;}result = new HashMap<>();try {// 加载"META-INF/spring.factories"配置文件Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);while (urls.hasMoreElements()) {URL url = urls.nextElement();UrlResource resource = new UrlResource(url);// 将配置信息封装成properties对象Properties properties = PropertiesLoaderUtils.loadProperties(resource);for (Map.Entry<?, ?> entry : properties.entrySet()) {String factoryTypeName = ((String) entry.getKey()).trim();String[] factoryImplementationNames =StringUtils.commaDelimitedListToStringArray((String) entry.getValue());for (String factoryImplementationName : factoryImplementationNames) {result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>()).add(factoryImplementationName.trim());}}}// Replace all lists with unmodifiable lists containing unique elementsresult.replaceAll((factoryType, implementations) -> implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));// 将结果添加到本地缓存中cache.put(classLoader, result);}catch (IOException ex) {throw new IllegalArgumentException("Unable to load factories from location [" +FACTORIES_RESOURCE_LOCATION + "]", ex);}return result;}

下面是spring.factores文件

# 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# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.autoconfigure.integration.IntegrationPropertiesEnvironmentPostProcessor# 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.Neo4jReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
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.neo4j.Neo4jAutoConfiguration,\
org.springframework.boot.autoconfigure.netty.NettyAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration,\
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.sql.init.SqlInitializationAutoConfiguration,\
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.ReactiveMultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebSessionIdResolverAutoConfiguration,\
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.jooq.NoDslContextBeanFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.MissingR2dbcPoolDependencyFailureAnalyzer,\
org.springframework.boot.autoconfigure.r2dbc.MultipleConnectionPoolConfigurationsFailureAnalzyer,\
org.springframework.boot.autoconfigure.r2dbc.NoConnectionFactoryBeanFailureAnalyzer,\
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# DataSource initializer detectors
org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector=\
org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializerDatabaseInitializerDetector# Depends on database initialization detectors
org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
org.springframework.boot.autoconfigure.batch.JobRepositoryDependsOnDatabaseInitializationDetector,\
org.springframework.boot.autoconfigure.quartz.SchedulerDependsOnDatabaseInitializationDetector,\
org.springframework.boot.autoconfigure.session.JdbcIndexedSessionRepositoryDependsOnDatabaseInitializationDetector

读取到spring.factories中的信息之后返回,然后在对读取到的内容进行去重,过滤等操作。

总结

  1. @SpringBootApplication注解下有三个注解,分别是@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan,自动装配的核心就是**@EnableAutoConfiguration**注解
  2. @EnableAutoConfiguration注解下面通过@Import注解注入了一个ImportSelector的子类AutoConfigurationImportSelector类,里面实现了selectImports方法。然后调用getAutoConfigurationEntry方法,然后是getCandidateConfigurations方法发,然后是loadFactoryNames方法,最后是loadSpringFactories方法,加载到META-INF/spring.factories文件中的自动配置类信息。
  3. 拿到自动配置类的信息之后,然后根据@EnableAutoConfiguration注解的 exclude 和 excludeName属性过滤 spring.factories 配置文件的内容。后面还会对配置信息进行去重,过滤等操作,获取最终的配置信息集合。
  4. 拿到过滤后的配置类信息之后,把这些类注册到Spring容器中,交给Spring容器进行管理。

相关文章:

SpringBoot面试之SpringBoot自动装配原理

SpringBoot自动装配原理 背景 最近因为各种原因&#xff0c;我又重新加入到了找工作的大军当中。昨天在面试的时候与面试官聊到我们项目都是基于SpringBoot开发的&#xff0c;然后面试官就顺口问了句&#xff1a;”SpringBoot项目会引入许多的starter&#xff0c;比如&#x…...

JavaScript:监听事件

该方法用于向浏览器窗口注册事件监听器&#xff0c;当指定的事件&#xff08;如单击、按键按下&#xff09;被触发时&#xff0c;浏览器会自动调用指定的函数&#xff08;回调函数&#xff09;。 window.addEventListener(event, function, useCapture); 参数说明&#xff1a…...

编写SQL语句,场景:从一张表中查询某字段是逗号分隔的集合值,需要遍历集合内每个值,将其作为条件去查询另一张表,最终返回列表

目录 场景编写SQL分页获取该开票单号下的所有订单列表使用子查询和 in 字句使用 find_in_set 场景 从一张表中查询某字段是逗号分隔的集合值&#xff0c;需要遍历集合内每个值&#xff0c;将其作为条件去查询另一张表&#xff0c;最终返回列表 编写SQL 分页获取该开票单号下…...

单链表相关面试题--7.链表的回文结构

7.链表的回文结构 链表的回文结构_牛客题霸_牛客网 (nowcoder.com) /* 解题思路&#xff1a; 此题可以先找到中间节点&#xff0c;然后把后半部分逆置&#xff0c;最近前后两部分一一比对&#xff0c;如果节点的值全部相同&#xff0c;则即为回文。 */ class PalindromeList…...

JUC(Java Util Concurrent)多线程并发库

JUC&#xff08;Java Util Concurrent&#xff09;是Java中用于编写多线程并发程序的库。开发过程中使用JUC主要有以下几个好处&#xff1a; 1. 提高程序性能&#xff1a;使用JUC可以实现多线程并发执行&#xff0c;充分利用多核CPU&#xff0c;提高程序的性能。 2. 简化代码…...

如何在Linux系统上检测GPU显存和使用情况?

如何在Linux系统上检测GPU显存和使用情况&#xff1f; 在Linux系统上&#xff0c;你可以使用一些命令行工具来检测GPU显存和使用情况。以下是一些常用的方法&#xff1a; 1. 使用nvidia-smi&#xff08;仅适用于NVIDIA GPU&#xff09; 如果你使用的是NVIDIA的显卡&#xff0…...

Django 入门学习总结5

修改polls/detail.html文件&#xff0c;写一个表单&#xff1a; <form action"{% url polls:vote question.id %}" method"post"> {% csrf_token %} <fieldset> <legend><h1>{{ question.question_text }}</h…...

FileNotFoundError: [Errno 2] No such file or directory: ‘patchelf‘: ‘patchelf‘

sudo apt-get install patchelf...

『new Date 在 IOS 失效 の bug』

问题&#xff1a;new Date()在安卓下正常&#xff0c;在IOS下显示不出来。 原因&#xff1a;在IOS下&#xff0c;new Date(“2000-2-22 00:10”),返回的是undefined&#xff0c;因为IOS不支持这种类型格式。 解决&#xff1a;更换下格式&#xff1a;new Date(“2000/2/22”) …...

macos创建xcframework及签名

前言 Framework 可以理解为封装了共享资源的具有层次结构的文件夹&#xff0c;共享资源可以是 nib文件、国际化字符串文件、头文件、库文件等等。它同时也是一个 Bundle&#xff0c;里面的内容可以通过 Bundle 相关 API 来访问。Framework 可以是 static framework 或 dynamic…...

Oracle与Redis Enterprise协同,作为企业缓存解决方案

来源&#xff1a;虹科云科技 虹科干货丨Oracle与Redis Enterprise协同&#xff0c;作为企业缓存解决方案 欢迎关注虹科&#xff0c;为您提供最新资讯&#xff01; 单独使用Oracle作为企业缓存数据库时&#xff0c;会出现哪些问题呢&#xff1f;使用Redis Enterprise与Oracle共…...

局部保持投影(Locality preserving projections,LPP)

局部保持投影&#xff08;Locality preserving projections&#xff0c;LPP&#xff09; 方法概述 核心思想 有映射 Y m ∗ n f ( X d ∗ n ) \underset{m*n}{Y}f(\underset {d*n}X) m∗nY​f(d∗nX​)&#xff0c;能够实现将d维的样本变换到m维空间之中 假设&#xff1a;对…...

Flutter:引领移动开发新潮流,跨平台应用程序的终极解决方案

文章目录 一、介绍二、环境搭建三、基础组件四、生命周期管理五、路由控制六、网络请求七、数据存储八、调试与优化《从零基础到精通Flutter开发》特色内容简介作者简介目录获取方式 一、介绍 Flutter是由Google开发的一款开源移动应用开发框架&#xff0c;它可以帮助开发者快…...

开源免费的流程设计器如何选型

大家在开发OA办公自动化、ERP、CRM、BPM、低代码平台等项目的时候&#xff0c;经常用到流程引擎&#xff0c;目前主流的开源流程引擎有activiti、flowable、camunda。这几个开源的流程引擎均基于BPMN2.0国际规范标准&#xff0c;其功能均比较强大&#xff0c;接口也很丰富。但涉…...

设置pdb自动启动

参考文档&#xff1a; How to Preserve Open Mode of PDBs When the CDB Restarts (Doc ID 1933511.1) -- 查看pdb的保留状态.无保留状态 select * from DBA_PDB_SAVED_STATES; SYScdbtest SQL> select * from DBA_PDB_SAVED_STATES;no rows selectedSYScdbtest SQL> -…...

抖店入驻成功后,新手需要怎么做?7天起店流程教会你!

我是电商珠珠 在抖店入驻成功后&#xff0c;很多新手并不知道怎么将抖店做起来。 我做抖店也已经3年时间了&#xff0c;期间也带着想做店的小伙伴一起&#xff0c;是第一波入驻抖店&#xff0c;并吃到抖店红利的商家。 虽然现在抖店起店比着去年、前年相对难了一些&#xff…...

RTS 客户端-服务器网络

Stone Monarch 从一开始就支持多人游戏&#xff0c;但随着时间的推移&#xff0c;网络模型经历了多次迭代。我最初基于这篇著名的帝国时代文章实现了点对点锁步模型。 点对点锁定步骤有一些众所周知的问题。点对点方面使玩家很难相互连接&#xff0c;并增加了每个新玩家的网络…...

python连接数据库的方式

python连接数据库的方式 pyzenith.connect&#xff08;&#xff09;函数就是连接数据库&#xff1b; exception.ScriptException&#xff08;&#xff09;这一句是自定义异常&#xff0c;可以不用我这个&#xff1b; finally里面还有一个try finally是有必要的&#xff0c;防止…...

【腾讯云云上实验室-向量数据库】探索腾讯云向量数据库:全方位管理与高效利用多维向量数据的引领者

目录 前言1 腾讯云向量数据库介绍2 向量数据库信息及设置2.1 向量数据库实例信息2.2 实例监控2.3 密钥管理2.4 安全组2.5 Embedding2.6 可视化界面 3 可视化界面4 Embedding4.1 embedding_coll精确查询4.2 unenabled_embedding_coll精确查询 5 数据库5.1 创建数据库5.2 插入数据…...

二、sql手工注入

一、SQL注入的本质 解释&#xff1a;想要进行sql注入&#xff0c;肯定要发现注入点&#xff0c;一般简单的sql注入通过下面两种方式判断就能发现是否存在sql注入漏洞 1.字符型 注意&#xff1a;字符型注入可能为或" 查询语句&#xff1a; select * from student where…...

YOLOv12涨点改进| TGRS 2026 | 独家创新首发、卷积改进篇| 引入轻量CKConv中国结卷积模块 ,适合小目标和细长目标的特征提取,助力小目标检测、小目标图像分割、低光图像增强任务涨点

一、本文介绍 🔥本文给大家介绍使用 CKConv中国结卷积模块 改进YOLOv12网络模型,通过在特征提取阶段更有效地增强暗弱小目标和细长目标的结构信息。其核心通过横向、纵向与方形卷积的组合,强化目标边缘、轮廓及中心响应,同时聚合周围弱像素信息,从而减少下采样过程中小目…...

马斯克起诉OpenAI开庭,索赔最高1800亿美元,案件责任认定5月21日结束

奥尔特曼、布罗克曼到场&#xff0c;马斯克未出席 当地时间4月27日上午&#xff0c;案件如期开庭&#xff0c;进入陪审团遴选阶段。据彭博社报道&#xff0c;奥尔特曼和布罗克曼亲自到场&#xff0c;这对于知名科技高管来说较为罕见&#xff0c;一般不会出庭&#xff0c;马斯克…...

YOLOv8模型评估进阶:修改metrics.py和val.py,解锁mAP75监控与可视化

YOLOv8模型评估进阶&#xff1a;深度定制mAP75监控与可视化全流程指南 在目标检测模型的迭代优化过程中&#xff0c;评估指标的选择直接影响着模型性能的判断标准。当mAP50达到90%以上的高饱和状态时&#xff0c;引入mAP75指标能够提供更严格的性能评估维度。本文将系统介绍如何…...

3DTilesRendererJS插件系统完全指南:扩展你的3D渲染能力

3DTilesRendererJS插件系统完全指南&#xff1a;扩展你的3D渲染能力 【免费下载链接】3DTilesRendererJS Renderer for 3D Tiles in Javascript using three.js, Babylon.js, and r3f 项目地址: https://gitcode.com/gh_mirrors/3d/3DTilesRendererJS 3DTilesRendererJS…...

从人耳听感到App音量调节:Android/iOS开发者必须懂的声压、分贝与振幅换算实战

移动端音频开发实战&#xff1a;从分贝调节到防Clipping的完整指南 当你滑动手机上的音量滑块时&#xff0c;是否思考过这简单的UI操作背后隐藏着怎样的声学原理&#xff1f;在开发音乐播放器、语音通话或游戏音效时&#xff0c;我们经常需要将用户直观的"音量减小6dB&quo…...

Qwen3.5-4B-AWQ应用场景:法律文书多语言比对+关键条款图文定位

Qwen3.5-4B-AWQ应用场景&#xff1a;法律文书多语言比对关键条款图文定位 1. 项目概述与技术优势 Qwen3.5-4B-AWQ-4bit是阿里云通义千问团队推出的轻量化大语言模型&#xff0c;特别适合法律行业的智能化应用。这个4bit量化的版本在保持强大能力的同时&#xff0c;大幅降低了…...

图片版权保护:芋田图像工具箱水印功能深度解析

在数字内容时代&#xff0c;图片被盗用的情况时有发生。 摄影作品、设计稿、原创配图&#xff0c;只要发布到网络上&#xff0c;就面临着被他人未经授权使用的风险。 对于原创内容创作者来说&#xff0c;如何有效保护自己的作品版权&#xff0c;是一个必须面对和解决的问题。 今…...

告别手动上传!用Python+SAP OData实现OA审批后自动同步请求号(保姆级避坑指南)

从OA审批到SAP请求号自动同步&#xff1a;Python与OData实战全解析 当审批流程在OA系统完成&#xff0c;而SAP系统中的请求号仍需手动录入时&#xff0c;这种割裂不仅消耗时间&#xff0c;更可能因人为疏忽导致数据不一致。我曾为某跨国企业实施自动化方案时&#xff0c;发现财…...

5分钟免费安装APA第7版:终极Microsoft Word参考文献格式指南

5分钟免费安装APA第7版&#xff1a;终极Microsoft Word参考文献格式指南 【免费下载链接】APA-7th-Edition Microsoft Word XSD for generating APA 7th edition references 项目地址: https://gitcode.com/gh_mirrors/ap/APA-7th-Edition 还在为论文参考文献格式被导师…...

FigmaCN:让中文设计师无障碍使用Figma的智能翻译插件

FigmaCN&#xff1a;让中文设计师无障碍使用Figma的智能翻译插件 【免费下载链接】figmaCN 中文 Figma 插件&#xff0c;设计师人工翻译校验 项目地址: https://gitcode.com/gh_mirrors/fi/figmaCN 你是否曾因Figma的英文界面而困扰&#xff1f;当设计团队需要快速协作时…...