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

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

1.3、按需开启自动配置项

虽然我们130个场景的所有自动配置启动的时候默认全部加载。但最终会按需配置
xxxxAutoConfiguration
按照条件装配规则(@Conditional),最终会按需配置。

1.3、修改默认配置

@Bean
@ConditionalOnBean(MultipartResolver.class)  //容器中有这个类型组件
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中没有这个名字 multipartResolver 的组件
public MultipartResolver multipartResolver(MultipartResolver resolver) {
    //给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
    //SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范
    // Detect if the user has created a MultipartResolver but named it incorrectly
    return resolver;

SpringBoot默认会在底层配好所有的组件。但是如果用户自己配置了以用户的优先

@Bean
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
 }

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

  • SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration
  • 每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值。xxxxProperties里面拿。xxxProperties和配置文件进行了绑定
  • 生效的配置类就会给容器中装配很多组件
  • 只要容器中有这些组件,相当于这些功能就有了
  • 定制化配置
  • 用户直接自己@Bean替换底层的组件
  • 用户去看这个组件是获取的配置文件什么值就去修改。
    xxxxxAutoConfiguration —>加载所有组件 —>组件从 xxxxProperties里面拿值 ----> 在application.properties自定义值

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

  • 查看springboot开发文档,根据开发文档修改配置文件。
  • 查看springboot底层源码,根据底层源码修改。
    如何根据springBoot底层源码配置application.properties配置文件?
    1、比如我们要配置有关缓存的信息,首先找到关于缓存自动配置的源码,一般都以xxxxAutoConfiguration命名的
    在这里插入图片描述2、找到CacheAutoConfiguration自动配置类。该类一般会使用@EnableConfigurationProperties注解与一个类绑定,有关缓存的配置都在CacheProperties中
    在这里插入图片描述3、在CacheProperties类定义了多个属性在这里插入图片描述4、我们在配置文件中以spring.cache.xxx(属性名)。就可以对缓存的信息进行修改配置。

在这里插入图片描述

1.7、springboot自动配置最佳实践

1、 引入场景依赖

查看官方文档看看可以引入哪些场景依赖

2、查看自动配置了哪些(选做)

  • 自己分析,引入场景对应的自动配置一般都生效了
  • 配置文件中debug=true开启自动配置报告。Negative(不生效)\Positive(生效)

3、 是否需要自行修改配置

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

1、springboot关于如何修改benner图的说明

在这里插入图片描述

2、springboot默认的banner是这个
在这里插入图片描述3、在application.properties配置文件中可以修改该banner图。
新建banner.txt。如果新的banner图文件名不是banner,则需要在application.properties中做配置。

# 修改banner图
spring.banner.image.location=classpath:aaa.jpg

4、在网上找一些自己喜欢的banner图。复制到banner.txt中
在这里插入图片描述重新启动主程序,banner已经发生修改。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值