Spring Boot的自动配置的原理
Posted 人生若只如初见yao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. 条件注解
以上是关于Spring Boot的自动配置的原理的主要内容,如果未能解决你的问题,请参考以下文章
[Spring Boot] 4. Spring Boot实现自动配置的原理
[Spring Boot] 4. Spring Boot实现自动配置的原理