SpringBoot | 自动配置原理

5次阅读

共计 17350 个字符,预计需要花费 44 分钟才能阅读完成。

微信公众号:一个优秀的废人。如有问题,请后台留言,反正我也不会听。
前言
这个月过去两天了,这篇文章才跟大家见面,最近比较累,大家见谅下。下班后闲着无聊看了下 SpringBoot 中的自动配置,把我的理解跟大家说下。
配置文件能写什么?
相信接触过 SpringBoot 的朋友都知道 SpringBoot 有各种 starter 依赖,想要什么直接勾选加进来就可以了。想要自定义的时候就直接在配置文件写自己的配置就好。但你们有没有困惑,为什么 SpringBoot 如此智能,到底配置文件里面能写什么呢?
带着这个疑问,我翻了下 SpringBoot 官网看到这么一些配置样例:

发现 SpringBoot 可配置的东西非常多,上图只是节选。有兴趣的查看这个网址:
https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-external-config-yaml
自动配置原理
这里我拿之前创建过的 SpringBoot 来举例讲解 SpringBoot 的自动配置原理,首先看这么一段代码:
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
毫无疑问这里只有 @SpringBootApplication 值得研究,进入 @SpringBootApplication 的源码:

SpringBoot 启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration,再进入 @EnableAutoConfiguration 源码:

发现最重要的就是 @Import(AutoConfigurationImportSelector.class) 这个注解,其中的 AutoConfigurationImportSelector 类的作用就是往 Spring 容器中导入组件,我们再进入这个类的源码,发现有这几个方法:
/**
* 方法用于给容器中导入组件
**/
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
autoConfigurationMetadata, annotationMetadata); // 获取自动配置项
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}

// 获取自动配置项
protected AutoConfigurationEntry getAutoConfigurationEntry(
AutoConfigurationMetadata autoConfigurationMetadata,
AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List < String > configurations = getCandidateConfigurations(annotationMetadata,
attributes); // 获取一个自动配置 List,这个 List 就包含了所有自动配置的类名
configurations = removeDuplicates(configurations);
Set < String > exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}

// 获取一个自动配置 List,这个 List 就包含了所有的自动配置的类名
protected List < String > getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
// 通过 getSpringFactoriesLoaderFactoryClass 获取默认的 EnableAutoConfiguration.class 类名,传入 loadFactoryNames 方法
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;
}

// 默认的 EnableAutoConfiguration.class 类名
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
代码注释很清楚:

首先注意到 selectImports 方法,其实从方法名就能看出,这个方法用于给容器中导入组件,然后跳到 getAutoConfigurationEntry 方法就是用于获取自动配置项的。
再来进入 getCandidateConfigurations 方法就是 获取一个自动配置 List,这个 List 就包含了所有的自动配置的类名。
再进入 SpringFactoriesLoader 类的 loadFactoryNames 方法,跳转到 loadSpringFactories 方法发现 ClassLoader 类加载器指定了一个 FACTORIES_RESOURCE_LOCATION 常量。
然后利用 PropertiesLoaderUtils 把 ClassLoader 扫描到的这些文件的内容包装成 properties 对象,从 properties 中获取到 EnableAutoConfiguration.class 类(类名)对应的值,然后把他们添加在容器中。

public static List < String > loadFactoryNames(Class < ? > factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryClassName, 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 {
// 扫描所有 jar 包类路径下 META-INF/spring.factories
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 properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry < ? , ? > entry : properties.entrySet()) {
String factoryClassName = ((String) entry.getKey()).trim();
for (String factoryName: StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
// 从 properties 中获取到 EnableAutoConfiguration.class 类(类名)对应的值,然后把他们添加在容器中
result.add(factoryClassName, factoryName.trim());
}
}
}
cache.put(classLoader, result);
return result;
} catch (IOException ex) {
throw new IllegalArgumentException(“Unable to load factories from location [” +
FACTORIES_RESOURCE_LOCATION + “]”, ex);
}
}
点击 FACTORIES_RESOURCE_LOCATION 常量,我发现它指定的是 jar 包类路径下 META-INF/spring.factories 文件:
public static final String FACTORIES_RESOURCE_LOCATION = “META-INF/spring.factories”;
将类路径下 META-INF/spring.factories 里面配置的所有 EnableAutoConfiguration 的值加入到了容器中,所有的 EnableAutoConfiguration 如下所示:注意到 EnableAutoConfiguration 有一个 = 号,= 号后面那一串就是这个项目需要用到的自动配置类。
# 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.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.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.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
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.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.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
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.groovy.template.GroovyTemplateAutoConfiguration,\
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.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.reactor.core.ReactorCoreAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityRequestMatcherProviderAutoConfiguration,\
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.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
每一个这样的 xxxAutoConfiguration 类都是容器中的一个组件,都加入到容器中,用他们来做自动配置。上述的每一个自动配置类都有自动配置功能,也可在配置文件中自定义配置。
举例说明 Http 编码自动配置原理
@Configuration
// 表示这是一个配置类,以前编写的配置文件一样,也可以给容器中添加组件

@EnableConfigurationProperties(HttpEncodingProperties.class)
// 启动指定类的 ConfigurationProperties 功能;将配置文件中对应的值和 HttpEncodingProperties 绑定起来;并把 HttpEncodingProperties 加入到 ioc 容器中

@ConditionalOnWebApplication
// Spring 底层 @Conditional 注解,根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效;判断当前应用是否是 web 应用,如果是,当前配置类生效

@ConditionalOnClass(CharacterEncodingFilter.class)
// 判断当前项目有没有这个类 CharacterEncodingFilter;SpringMVC 中进行乱码解决的过滤器;

@ConditionalOnProperty(prefix = “spring.http.encoding”, value = “enabled”, matchIfMissing = true)
// 判断配置文件中是否存在某个配置 spring.http.encoding.enabled;如果不存在,判断也是成立的
// 即使我们配置文件中不配置 pring.http.encoding.enabled=true,也是默认生效的;
public class HttpEncodingAutoConfiguration {

// 已经和 SpringBoot 的配置文件建立映射关系了
private final HttpEncodingProperties properties;

// 只有一个有参构造器的情况下,参数的值就会从容器中拿
public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
this.properties = properties;
}

@Bean
// 给容器中添加一个组件,这个组件的某些值需要从 properties 中获取
@ConditionalOnMissingBean(CharacterEncodingFilter.class)
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}
自动配置类做了什么不在这里赘述,请见上面代码。所有在配置文件中能配置的属性都是在 xxxxProperties 类中封装的;配置文件能配置什么就可以参照某个功能对应的这个属性类,例如上述提到的 @EnableConfigurationProperties(HttpProperties.class),我们打开 HttpProperties 文件源码节选:
@ConfigurationProperties(prefix = “spring.http”)
public class HttpProperties {

public static class Encoding {

public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* Charset of HTTP requests and responses. Added to the “Content-Type” header if
* not set explicitly.
*/
private Charset charset = DEFAULT_CHARSET;

/**
* Whether to force the encoding to the configured charset on HTTP requests and
* responses.
*/
private Boolean force;

}
在上面可以发现里面的属性 charset、force 等,都是我们可以在配置文件中指定的,它的前缀就是 spring.http.encoding 如:

另外,如果配置文件中有配该属性就取配置文件的,若无就使用 XxxxProperties.class 文件的默认值,比如上述代码的 Charset 属性,如果不配那就使用 UTF-8 默认值。
总结
1. SpringBoot 启动会加载大量的自动配置类
2. 我们看我们需要的功能有没有 SpringBoot 默认写好的自动配置类;
3. 我们再来看这个自动配置类中到底配置了哪些组件 (只要我们要用的组件有,我们就不需要再来配置,若没有,我们可能就要考虑自己写一个配置类让 SpringBoot 扫描了)
4. 给容器中自动配置类添加组件的时候,会从 properties 类中获取某些属性。我们就可以在配置文件中指定这些属性的值;
xxxxAutoConfigurartion 自动配置类的作用就是给容器中添加组件
xxxxProperties 的作用就是封装配置文件中相关属性
至此,总算弄明白了 SpringBoot 的自动配置原理。我水平优先,如有不当之处,敬请指出,相互交流学习,希望对你们有帮助。
后语
如果本文对你哪怕有一丁点帮助,请帮忙点好看。你的好看是我坚持写作的动力。
另外,关注之后在发送 1024 可领取免费学习资料。
资料详情请看这篇旧文:Python、C++、Java、Linux、Go、前端、算法资料分享

正文完
 0

SpringBoot自动配置原理

5次阅读

共计 3317 个字符,预计需要花费 9 分钟才能阅读完成。

前言
只有光头才能变强。
文本已收录至我的 GitHub 仓库,欢迎 Star:https://github.com/ZhongFuCheng3y/3y

回顾前面 Spring 的文章(以学习的顺序排好):

Spring 入门这一篇就够了
Spring【依赖注入】就是这么简单
Spring【AOP 模块】就这么简单
Spring【DAO 模块】知识要点
SpringMVC 入门就这么简单
SpringMVC【开发 Controller】详解
SpringMVC【参数绑定、数据回显、文件上传】
SpringMVC【校验器、统一处理异常、RESTful、拦截器】
SpringBoot 就是这么简单
SpringData JPA 就是这么简单
Spring IOC 知识点一网打尽!
Spring AOP 就是这么简单啦
外行人都能看懂的 SpringCloud,错过了血亏!

作为一名 Java 程序员,就不可能不了解 SpringBoot,如果不了解(赶紧学!)
一、SpringBoot 的自动配置原理
不知道大家第一次搭 SpringBoot 环境的时候,有没有觉得非常简单。无须各种的配置文件,无须各种繁杂的 pom 坐标,一个 main 方法,就能 run 起来了。与其他框架整合也贼方便,使用 EnableXXXXX 注解就可以搞起来了!
所以今天来讲讲 SpringBoot 是如何实现自动配置的~
1.1 三个重要的注解
我们可以发现,在使用 main()启动 SpringBoot 的时候,只有一个注解 @SpringBootApplication

我们可以点击进去 @SpringBootApplication 注解中看看,可以发现有三个注解是比较重要的:

@SpringBootConfiguration:我们点进去以后可以发现底层是 Configuration 注解,说白了就是支持 JavaConfig 的方式来进行配置(使用 Configuration 配置类等同于 XML 文件)。

@EnableAutoConfiguration:开启自动配置功能(后文详解)

@ComponentScan:这个注解,学过 Spring 的同学应该对它不会陌生,就是扫描注解,默认是扫描当前类下的 package。将 @Controller/@Service/@Component/@Repository 等注解加载到 IOC 容器中。

所以,Java3yApplication 类可以被我们当做是这样的:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class Java3yApplication {

public static void main(String[] args) {
SpringApplication.run(Java3yApplication.class, args);
}
}
1.2 重点 EnableAutoConfiguration
我们知道 SpringBoot 可以帮我们减少很多的配置,也肯定听过“约定大于配置”这么一句话,那 SpringBoot 是怎么做的呢?其实靠的就是 @EnableAutoConfiguration 注解。
简单来说,这个注解可以帮助我们自动载入应用程序所需要的所有默认配置。
介绍有一句说:
if you have tomcat-embedded.jar on your classpath you are likely to want a TomcatServletWebServerFactory
如果你的类路径下有 tomcat-embedded.jar 包,那么你很可能就需要 TomcatServletWebServerFactory
我们点进去看一下,发现有两个比较重要的注解:

@AutoConfigurationPackage:自动配置包

@Import:给 IOC 容器导入组件

1.2.1AutoConfigurationPackage
网上将这个 @AutoConfigurationPackage 注解解释成自动配置包,我们也看看 @AutoConfigurationPackage 里边有什么:

我们可以发现,依靠的还是 @Import 注解,再点进去查看,我们发现重要的就是以下的代码:

@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
register(registry, new PackageImport(metadata).getPackageName());
}

在默认的情况下就是将:主配置类 (@SpringBootApplication) 的所在包及其子包里边的组件扫描到 Spring 容器中。
看完这句话,会不会觉得,这不就是 ComponentScan 的功能吗?这俩不就重复了吗?
我开始也有这个疑问,直到我看到文档的这句话:
it will be used when scanning for code @Entity classes.It is generally recommended that you place EnableAutoConfiguration (if you’renot using @SpringBootApplication) in a root package so that all sub-packagesand classes can be searched.
比如说,你用了 Spring Data JPA,可能会在实体类上写 @Entity 注解。这个 @Entity 注解由 @AutoConfigurationPackage 扫描并加载,而我们平时开发用的 @Controller/@Service/@Component/@Repository 这些注解是由 ComponentScan 来扫描并加载的。
简单理解:这二者扫描的对象是不一样的。
1.2.2 回到 Import
我们回到 @Import(AutoConfigurationImportSelector.class)这句代码上,再点进去 AutoConfigurationImportSelector.class 看看具体的实现是什么:

我们再进去看一下这些配置信息是从哪里来的(进去 getCandidateConfigurations 方法):

这里包装了一层,我们看到的只是通过 SpringFactoriesLoader 来加载,还没看到关键信息,继续进去:

简单梳理:

FACTORIES_RESOURCE_LOCATION 的值是 META-INF/spring.factories

Spring 启动的时候会扫描所有 jar 路径下的 META-INF/spring.factories,将其文件包装成 Properties 对象
从 Properties 对象获取到 key 值为 EnableAutoConfiguration 的数据,然后添加到容器里边。

最后我们会默认加载 113 个默认的配置类:

有兴趣的同学可以去翻一下这些文件以及配置类哦:

1.3 总结
@SpringBootApplication 等同于下面三个注解:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

其中 @EnableAutoConfiguration 是关键(启用自动配置),内部实际上就去加载 META-INF/spring.factories 文件的信息,然后筛选出以 EnableAutoConfiguration 为 key 的数据,加载到 IOC 容器中,实现自动配置功能!

官网文档参考:
https://docs.spring.io/spring-boot/docs/2.2.0.BUILD-SNAPSHOT/reference/html/using-spring-boot.html#using-boot-structuring-your-code
英语不好的同学可以像我一样,对照着来看:

最后
乐于输出干货的 Java 技术公众号:Java3y。公众号内有 200 多篇原创技术文章、海量视频资源、精美脑图,不妨来关注一下!

觉得我的文章写得不错,不妨点一下赞!

正文完
 0