一、SpringBoot2基础入门--07-SpringBoot自动配置原理

本文详细解释了SpringBoot的自动配置原理,包括常用注解如@ConditionalOnBean、@ConditionalOnClass和@EnableConfigurationProperties的使用,以及如何通过@SpringBootApplication注解管理和修改配置。还介绍了如何查看和管理SpringBoot启动时的自动配置过程。
摘要由CSDN通过智能技术生成
  • 1.3、按需开启自动配置项

  • 1.3、修改默认配置

  • 1.5、总结springboot的自动配置原理

  • 1.6、怎么修改SpringBoot配置文件,配置文件可以修改哪些内容?

  • 1.7、springboot自动配置最佳实践

  • 1.8、如何修改spring启动时默认的banner图

1、自动配置原理

=======================================================================

学习参考文档

1.1、springboot自动配置底层常用注解


在这里插入图片描述

1.1.1、@ConditionalOnMissingBean与@ConditionalOnBean

@ConditionalOnMissingBean容器中不存在这个类型的组件时,下面内容才生效;

@ConditionalOnBean容器中存在这个类型的组件时,下面内容才生效。

在这里插入图片描述

1.1.2、@ConditionalOnClass与ConditionalOnMissingClass

@ConditionalOnClass(CharacterEncodingFilter.class)项目中引入了这个类(CharacterEncodingFilter)时,下面内容才生效;

@ConditionalOnMissingClass(CharacterEncodingFilter.class)项目中没有引入了这个类(CharacterEncodingFilter)时,下面内容才生效;

在这里插入图片描述

1.1.3、@EnableConfigurationProperties

  • @EnableConfigurationProperties(ServerProperties.class)

  • 用于开启属性配置绑定,属性在ServerProperties.class类中,如下图所示,ServerProperties类中定义了多个属性。

在这里插入图片描述

  • 我们可以在配置文件application.prperties中修改这些属性的值。

在这里插入图片描述

1.2、主程序中的@SpringBootApplication注解


@SpringBootApplication注解,主程序类上使用的注解。该注解可以替换一下三个注解。

@SpringBootConfiguration

@EnableAutoConfiguration

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

public @interface SpringBootApplication{}

1.2.1、@SpringBootConfiguration

@Configuration。代表当前是一个配置类,说明springboot的主程序也是一个配置类

在这里插入图片描述

1.2.2、@ComponentScan

指定扫描哪些包,Spring注解;

在这里插入图片描述

1.2.3、@EnableAutoConfiguration

@AutoConfigurationPackage

@Import(AutoConfigurationImportSelector.class)

public @interface EnableAutoConfiguration {}

  • 1、@AutoConfigurationPackage

自动配置包?指定了默认的包规则

@Import(AutoConfigurationPackages.Registrar.class) //给容器中导入一系列组件

public @interface AutoConfigurationPackage {}

//利用Registrar给容器中导入一系列组件。批量导入组件

//将指定的一个包下的所有组件导入进来?在本程序中指该包指MainApplication 类所在包,即com.study包。

在这里插入图片描述

在这里插入图片描述

  • 2、@Import(AutoConfigurationImportSelector.class)

(1)利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件

在这里插入图片描述(2)调用List configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类

在这里插入图片描述

文件里面写死了spring-boot一启动就要给容器中加载的所有配置类

spring-boot-autoconfigure-2.3.4.RELEASE.jar/META-INF/spring.factories

Auto Configure

0 = “org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration”

1 = “org.springframework.boot.autoconfigure.aop.AopAutoConfiguration”

2 = “org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration”

3 = “org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration”

4 = “org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration”

5 = “org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration”

6 = “org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration”

7 = “org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration”

8 = “org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration”

9 = “org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration”

10 = “org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration”

11 = “org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration”

12 = “org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration”

13 = “org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration”

14 = “org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration”

15 = “org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration”

16 = “org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration”

17 = “org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration”

18 = “org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration”

19 = “org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration”

20 = “org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration”

21 = “org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration”

22 = “org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration”

23 = “org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration”

24 = “org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration”

25 = “org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration”

26 = “org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration”

27 = “org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration”

28 = “org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration”

29 = “org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration”

30 = “org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration”

31 = “org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration”

32 = “org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveDataAutoConfiguration”

33 = “org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveRepositoriesAutoConfiguration”

34 = “org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration”

35 = “org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration”

36 = “org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration”

37 = “org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration”

38 = “org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration”

39 = “org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration”

40 = “org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration”

41 = “org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration”

42 = “org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration”

43 = “org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration”

44 = “org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration”

45 = “org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration”

46 = “org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration”

47 = “org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration”

48 = “org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration”

49 = “org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration”

50 = “org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration”

51 = “org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration”

52 = “org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration”

53 = “org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration”

54 = “org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration”

55 = “org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration”

56 = “org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration”

57 = “org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration”

58 = “org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration”

59 = “org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration”

60 = “org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration”

61 = “org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration”

62 = “org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration”

63 = “org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration”

64 = “org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration”

65 = “org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration”

66 = “org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration”

67 = “org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration”

68 = “org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration”

69 = “org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration”

70 = “org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration”

71 = “org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration”

72 = “org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration”

73 = “org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration”

74 = “org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration”

75 = “org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration”

76 = “org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration”

77 = “org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration”

78 = “org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration”

79 = “org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration”

80 = “org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration”

81 = “org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration”

82 = “org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration”

83 = “org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration”

84 = “org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration”

85 = “org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration”

86 = “org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration”

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”

100 = “org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration”

101 = “org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration”

102 = “org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration”

103 = “org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration”

104 = “org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration”

105 = “org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration”

106 = “org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration”

107 = “org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration”

108 = “org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration”

109 = “org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration”

110 = “org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration”

111 = “org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration”

112 = “org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration”

113 = “org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration”

114 = “org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration”

115 = “org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration”

116 = “org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration”

117 = “org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration”

118 = “org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration”

119 = “org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration”

120 = “org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration”

121 = “org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration”

122 = “org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration”

123 = “org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration”

124 = “org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration”

125 = “org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration”

126 = “org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration”

127 = “org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration”

128 = “org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration”

129 = “org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration”

(3)利用工厂加载 Map<String, List> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件

(4)从META-INF/spring.factories位置来加载一个文件。

默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件

spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

由于细节内容实在太多了,为了不影响文章的观赏性,只截出了一部分知识点大致的介绍一下,每个小节点里面都有更细化的内容!

小编准备了一份Java进阶学习路线图(Xmind)以及来年金三银四必备的一份《Java面试必备指南》

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
得这些内容对你有帮助,可以扫码获取!!(备注Java获取)**

img

最后

由于细节内容实在太多了,为了不影响文章的观赏性,只截出了一部分知识点大致的介绍一下,每个小节点里面都有更细化的内容!

[外链图片转存中…(img-dzWYUBU5-1713429088240)]

小编准备了一份Java进阶学习路线图(Xmind)以及来年金三银四必备的一份《Java面试必备指南》

[外链图片转存中…(img-XfjLbEfZ-1713429088241)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值