Spring Boot的自动配置的原理

Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器。

1.1.1.   Maven下载源码

通过 dependency:sources 该命令可以下载该项目中所有的依赖的包的源码。

1.1.2.   源码分析

org.springframework.boot.SpringApplication:

org.springframework.core.io.support.SpringFactoriesLoader:

由此可见,读取该配置文件来加载内容。

1.1.3.   Spring.factories文件

  1 # Initializers
  2
  3 org.springframework.context.ApplicationContextInitializer=  4
  5 org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,  6
  7 org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
  8
  9
 10
 11 # Application Listeners
 12
 13 org.springframework.context.ApplicationListener= 14
 15 org.springframework.boot.autoconfigure.BackgroundPreinitializer
 16
 17
 18
 19 # Auto Configuration Import Listeners
 20
 21 org.springframework.boot.autoconfigure.AutoConfigurationImportListener= 22
 23 org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
 24
 25
 26
 27 # Auto Configuration Import Filters
 28
 29 org.springframework.boot.autoconfigure.AutoConfigurationImportFilter= 30
 31 org.springframework.boot.autoconfigure.condition.OnClassCondition
 32
 33
 34
 35 # Auto Configure
 36
 37 org.springframework.boot.autoconfigure.EnableAutoConfiguration= 38
 39 org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration, 40
 41 org.springframework.boot.autoconfigure.aop.AopAutoConfiguration, 42
 43 org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration, 44
 45 org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration, 46
 47 org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, 48
 49 org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, 50
 51 org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration, 52
 53 org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration, 54
 55 org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration, 56
 57 org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration, 58
 59 org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration, 60
 61 org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration, 62
 63 org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, 64
 65 org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, 66
 67 org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration, 68
 69 org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration, 70
 71 org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration, 72
 73 org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, 74
 75 org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, 76
 77 org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, 78
 79 org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration, 80
 81 org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration, 82
 83 org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, 84
 85 org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, 86
 87 org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, 88
 89 org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, 90
 91 org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration, 92
 93 org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration, 94
 95 org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, 96
 97 org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration, 98
 99 org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,100
101 org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,102
103 org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,104
105 org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,106
107 org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,108
109 org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,110
111 org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,112
113 org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,114
115 org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,116
117 org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,118
119 org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,120
121 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,122
123 org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,124
125 org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,126
127 org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,128
129 org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,130
131 org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,132
133 org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,134
135 org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,136
137 org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,138
139 org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,140
141 org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,142
143 org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,144
145 org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,146
147 org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,148
149 org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,150
151 org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,152
153 org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,154
155 org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,156
157 org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,158
159 org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,160
161 org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,162
163 org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,164
165 org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,166
167 org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,168
169 org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,170
171 org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,172
173 org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,174
175 org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,176
177 org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,178
179 org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,180
181 org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,182
183 org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,184
185 org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,186
187 org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,188
189 org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,190
191 org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,192
193 org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,194
195 org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,196
197 org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,198
199 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,200
201 org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,202
203 org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,204
205 org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,206
207 org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,208
209 org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,210
211 org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,212
213 org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,214
215 org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,216
217 org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,218
219 org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,220
221 org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,222
223 org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,224
225 org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,226
227 org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,228
229 org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
230
231
232
233 # Failure analyzers
234
235 org.springframework.boot.diagnostics.FailureAnalyzer=236
237 org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,238
239 org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,240
241 org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer
242
243
244
245 # Template availability providers
246
247 org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=248
249 org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,250
251 org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,252
253 org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,254
255 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,256
257 org.springframework.boot.autoconfigure.web.JspTemplateAvailabilityProvider

1.1.4.   举例:Redis的自动配置

从上述的配置中可以看出,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration是Redis的自动配置。

内容:

1.1.5.   条件注解

时间: 2024-11-05 01:02:01

Spring Boot的自动配置的原理的相关文章

spring boot 系列之六:@Conditional和spring boot的自动配置

我们知道,spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate是不是在Classpath里面?如果是,并且DataSource也存在,就自动配置一个JdbcTemplate的Bean Thymeleaf是不是在Classpath里面?如果是,则自动配置Thymeleaf的模板解析器.视图解析器.模板引擎 那个这个是怎么实现的呢?原因就在于它利用了Spring的条件化配置,条件化配置允许配置存在于应用中

SpringBoot自动配置的原理

Spring Boot的运行是由注解@EnableAutoConfiguration提供的它的关键功能是@Import注解.        EnableAutoConfigurationImportSelector 使用 SpringFactoriesLoader.loadFactoryNames 方法来扫描具有MEAT-INF/spring.factories文件的jar包 在了解原理前我们需要先认识下面几个注解:        @ConditionalOnBean:当容器里有指定的Bean为

Spring boot中自动编译配置

MAVEN自动编译配置 热部署功能 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> Spring Boot的Plugin配置能够将应用自动打包为war或者jar应用.添加bui

Spring boot 国际化自动加载资源文件问题

Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义正确的资源文件路径,Spring boot就启动时就会自动加载资源. spring.messages.basename=i18n/message 但是我的项目修改后获取消息时系统报错,找不到对应语言的资源配置.于是试图找到原因.Google好久都没找到,简直好像就我一个人遇到这鬼问题一样??.只好自

Spring Boot SSL [https]配置例子

前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server.port=8443 server.ssl.key-alias=selfsigned_localhost_sslserver server.ssl.key-password=changeit server.ssl.key-store=classpath:ssl-server.jks server.ss

Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties

目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationPropertiesAutoConfiguration 4.总结 3.外部化配置的核心 ????????接着上一章,<Spring Boot 外部化配置(一)> 3.2 @ConfigurationProperties 众所周知,当 Spring Boot 集成外部组件后,就可在 propertie

Spring Boot 全局异常配置

Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中.使用 ControllerAdvice 注解 package com.li.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import

Spring Boot 2.x教程-Thymeleaf 原理是什么

layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boot 2.x教程-Thymeleaf 原理是什么 keywords: SpringBoot, Spring, Thymeleaf --- 如要要理清楚 Thymeleaf 的原理,那么就要从模板引擎的原理说起.Thymeleaf只不过是众多模板中的一员,功能是一致的. 例如 JSP 也是一种模板. 1

Spring Boot项目属性配置

接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将server的端口变为8888,路径加上HelloWorld: 在DeomApplication.java的页面时点击运行按钮,打开浏览器输入:http://localhost:8888/HelloWorld/hello 此时,控制台的输出信息也可以看到端口变成8888了: 之前的url已无效: 更改后