返回介绍

10.2 Spring Boot 自动配置原理

发布于 2025-04-26 13:26:36 字数 3923 浏览 0 评论 0 收藏

所有的自动配置都是从注解 @SpringBootApplication 引入的,我们来看看它的源代码,就一切都明白了。如代码清单 10-5 所示,注解 @SpringBootApplication 其实又包含了三个非常重要的注解,即 @Configuration、@EnableAutoConfiguration 和 @ComponentScan,其中注解 @EnableAutoConfiguration 就是启用自动配置的,并将导入一些自动配置的类定义,注解 @ComponentScan 将扫描和加载应用中的一些自定义的类。

代码清单 10-5 SpringBootApplication 源代码

package org.springframework.boot.autoconfigure;
......
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
    ......
}

10.2.1 自动配置的即插即用原理

EnableAutoConfiguration 最终会导入一个自动配置的类列表,如代码清单 10-6 所示。列表中的自动配置类很多,这些配置类中大都将被导入,并处于备用状态中,这如同电器中准备了一些插槽一样,即实现了即插即用的原理。这样,当项目中引入了相关的包时,相关的功能将被启用。例如在项目的 Maven 管理中配置了 Redis 的引用,那么 Redis 的功能将被启用,这时启动应用,程序将尝试读取有关 Redis 的配置信息。

代码清单 10-6 自动配置类部分列表

# 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.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
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.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\……





10.2.2 自动配置的约定优先原理

在自动配置中加载一个类的配置时,首先读取项目中的配置,只有项目中没有相关配置才启用配置的默认值,这就是自动配置的约定优先原理。代码清单 10-7 是 Thymeleaf 配置类的源代码,如果在项目的配置文件中没有配置 spring.thymeleaf 的相关参数,就使用 Thymeleaf 的默认配置,默认配置将使用 templates 作为 HTML 文件的存放路径。在前面章节使用 Thymeleaf 的实例中,就是使用了这个默认配置。

代码清单 10-7 Thymeleaf 配置源代码

package org.springframework.boot.autoconfigure.thymeleaf;
......
@ConfigurationProperties("spring.thymeleaf")
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/
html");
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML5";
    private Charset encoding;
    private MimeType contentType;
    private boolean cache;
    private Integer templateResolverOrder;
    private String[] viewNames;
    private String[] excludedViewNames;
    private boolean enabled;
    ......
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。