【Springboot2.x】源码分析-@SpringBootApplication+AutoConfiguration自动配置流程

一、源码分析过程

@SpringBootApplication
public class SpringBoot2xApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringBoot2xApplication.class, args);
        Map<String, Cat> beansOfType = run.getBeansOfType(Cat.class);
        for (String key : beansOfType.keySet() ) {
            System.out.println("★★★★★★---》" +key+":" +beansOfType.get(key));
        }
       // System.out.println(beansOfType.get("User01")==beansOfType.get("com.tiandy.springboot2_x.User"));
    }
}

1.1 @SpringBootApplication 是一个符合注解

按住ctrl点击 @SpringBootApplication 后
在这里插入图片描述
@EnableAutoConfiguration
在这里插入图片描述

第一步:@AutoConfigurationPackage
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第二步:加载所有的自动配置组件 @Import({AutoConfigurationImportSelector.class})
利用如下方法获取所有的配置
getAutoConfigurationEntry(AnnotationMetadata annotationMetadata)
在这里插入图片描述
this.getCandidateConfigurations 点进去
在这里插入图片描述
在这里插入图片描述

jar:file:/D:/mavenrespo/org/springframework/spring-beans/5.2.15.RELEASE/spring-beans-5.2.15.RELEASE.jar!/META-INF/spring.factories
file:/D:/mavenrespo/org/springframework/boot/spring-boot-devtools/2.3.12.RELEASE/spring-boot-devtools-2.3.12.RELEASE.jar!/META-INF/spring.factories
file:/D:/mavenrespo/org/springframework/boot/spring-boot/2.3.12.RELEASE/spring-boot-2.3.12.RELEASE.jar!/META-INF/spring.factories
file:/D:/mavenrespo/org/springframework/boot/spring-boot-autoconfigure/2.3.12.RELEASE/spring-boot-autoconfigure-2.3.12.RELEASE.jar!/META-INF/spring.factories
file:/D:/mavenrespo/org/springframework/boot/spring-boot-test/2.3.12.RELEASE/spring-boot-test-2.3.12.RELEASE.jar!/META-INF/spring.factories
file:/D:/mavenrespo/org/springframework/boot/spring-boot-test-autoconfigure/2.3.12.RELEASE/spring-boot-test-autoconfigure-2.3.12.RELEASE.jar!/META-INF/spring.factories
file:/D:/mavenrespo/org/springframework/spring-test/5.2.15.RELEASE/spring-test-5.2.15.RELEASE.jar!/META-INF/spring.factories

通过搜索,以上的jar保重包含spring.factories文件 加载所有的spring.factories文件下的bean

在这里插入图片描述
在这里插入图片描述
例如:
在这里插入图片描述
搜先是加载所有的spring.factories中配置的组件,后期会经过条件判断

在这里插入图片描述

configurations = {LinkedList@1975}  size = 130
 0 = "org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration"
 1 = "org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration"
 2 = "org.springframework.boot.devtools.autoconfigure.RemoteDevToolsAutoConfiguration"
 3 = "org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration"
 4 = "org.springframework.boot.autoconfigure.aop.AopAutoConfiguration"
 5 = "org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration"
 6 = "org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration"
 7 = "org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration"
 8 = "org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration"
 9 = "org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration"
 10 = "org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration"
 11 = "org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration"
 12 = "org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration"
 13 = "org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration"
 14 = "org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration"
 15 = "org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration"
 16 = "org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration"
 17 = "org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration"
 18 = "org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration"
 19 = "org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration"
 20 = "org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration"
 21 = "org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration"
 22 = "org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration"
 23 = "org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration"
 24 = "org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration"
 25 = "org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration"
 26 = "org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration"
 27 = "org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration"
 28 = "org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration"
 29 = "org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration"
 30 = "org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration"
 31 = "org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration"
 32 = "org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration"
 33 = "org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration"
 34 = "org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration"
 35 = "org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration"
 36 = "org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration"
 37 = "org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration"
 38 = "org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration"
 39 = "org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration"
 40 = "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration"
 41 = "org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration"
 42 = "org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration"
 43 = "org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration"
 44 = "org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration"
 45 = "org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration"
 46 = "org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration"
 47 = "org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration"
 48 = "org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration"
 49 = "org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration"
 50 = "org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration"
 51 = "org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration"
 52 = "org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration"
 53 = "org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration"
 54 = "org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration"
 55 = "org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration"
 56 = "org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration"
 57 = "org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration"
 58 = "org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration"
 59 = "org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"
 60 = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
 61 = "org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration"
 62 = "org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration"
 63 = "org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration"
 64 = "org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration"
 65 = "org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration"
 66 = "org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration"
 67 = "org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration"
 68 = "org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration"
 69 = "org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration"
 70 = "org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration"
 71 = "org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration"
 72 = "org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration"
 73 = "org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration"
 74 = "org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration"
 75 = "org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration"
 76 = "org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration"
 77 = "org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration"
 78 = "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"
 79 = "org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration"
 80 = "org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration"
 81 = "org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration"
 82 = "org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration"
 83 = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration"
 84 = "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration"
 85 = "org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration"
 86 = "org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration"
 87 = "org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration"
 88 = "org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration"
 89 = "org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration"
 90 = "org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration"
 91 = "org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration"
 92 = "org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration"
 93 = "org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration"
 94 = "org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration"
 95 = "org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration"
 96 = "org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration"
 97 = "org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration"
 98 = "org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration"
 99 = "org.springframework.boot.autoconfigure.session.SessionAutoConfiguration"

二、★自动配置流程

第一步:通过maven包中查找所有spring.factories 文件,扫描到所有的配置,自动配置会加载这种XXXAutoConfiguration类。
第二步:进行Conditional条件判断,把符合条件的bean加载到ioc容器中。
在这里插入图片描述
第三步:只要容器中有了这个组件,相当于相应的功能就有了。
第四步:定制化配置
1)用户去自定义容器在这里插入图片描述 2) 通过配置类添加
在这里插入图片描述
在这里插入图片描述
三、如何进行配置
1)通过查询配置文档配置:https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/html/appendix-application-properties.html#common-application-properties-json
2)直接通过查找配置类进行配置:
https://blog.csdn.net/fen_dou_shao_nian/article/details/123811292
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3)检查配置是否生效
application.properties 中配置 debug=true
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值