关于Spring Boot自动配置,在本知识库的“Hello WOrld入门 —- Hello World探究”部分有作整体简介。 自动配置具体能配置哪些参数,可以参见官网

    一、启动

    Spring Boot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration


    二、@EnableAutoConfiguration**

    可以利用EnableAutoConfigurationImportSelector像容器中导入组件,selectImports()中则指明了能导入那些组件。

    1. // 获取候选的配置
    2. List configurations = getCandidateConfigurations(annotationMetadata, attributes);
    3. /**
    4. * 扫描所有jar包类路径下META-INF/spring.factories,把扫描到的这些文件的内容包装成properties对象
    5. * 从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中
    6. **/
    7. SpringFactoriesLoader.loadFactoryNames()

    将类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中

    1. # Auto Configure
    2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    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.cloud.CloudAutoConfiguration,\
    10. org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
    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.ElasticsearchAutoConfiguration,\
    24. org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
    25. org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
    26. org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
    27. org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\
    28. org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
    29. org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
    30. org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
    31. org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
    32. org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
    33. org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
    34. org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
    35. org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
    36. org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
    37. org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
    38. org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
    39. org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
    40. org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
    41. org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
    42. org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
    43. org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
    44. org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
    45. org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
    46. org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
    47. org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
    48. org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
    49. org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
    50. org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
    51. org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
    52. org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
    53. org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
    54. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
    55. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
    56. org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
    57. org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
    58. org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
    59. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
    60. org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
    61. org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
    62. org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
    63. org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
    64. org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
    65. org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
    66. org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
    67. org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
    68. org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
    69. org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
    70. org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
    71. org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
    72. org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
    73. org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
    74. org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
    75. org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
    76. org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
    77. org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
    78. org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
    79. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
    80. org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
    81. org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
    82. org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
    83. org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
    84. org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
    85. org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
    86. org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
    87. org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
    88. org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
    89. org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientAutoConfiguration,\
    90. org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
    91. org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
    92. org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
    93. org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
    94. org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
    95. org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
    96. org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
    97. org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
    98. org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
    99. org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
    100. org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
    101. org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
    102. org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
    103. org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
    104. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
    105. org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
    106. org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
    107. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
    108. org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
    109. org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
    110. org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
    111. org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

    每一个这样的 xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中,用他们来做自动配置。

    三、给每一个自动配置类进行自动配置功能

    以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理

    1. // 表示这是一个配置类,和以前编写的配置文件一样,也可以给容器中添加组件
    2. @Configuration
    3. /**
    4. * 启动指定类的ConfigurationProperties功能,将配置文件中对应的值和HttpEncodingProperties绑定起来,
    5. * 并把HttpEncodingProperties加入到ioc容器中
    6. **/
    7. @EnableConfigurationProperties(HttpEncodingProperties.class)
    8. /**
    9. * Spring底层@Conditional注解,根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效
    10. * 判断当前应用是否是web应用,如果是当前配置类生效
    11. **/
    12. @ConditionalOnWebApplication
    13. /**
    14. * 判断当前项目有没有这个类CharacterEncodingFilter,SpringMVC中进行乱码解决的过滤器
    15. **/
    16. @ConditionalOnClass(CharacterEncodingFilter.class)
    17. /**
    18. * 判断配置文件中是否存在某个配置spring.http.encoding.enabled,如果不存在,判断也是成立的
    19. * 即使我们配置文件中不配置pring.http.encoding.enabled=true,也是默认生效的
    20. **/
    21. @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
    22. public class HttpEncodingAutoConfiguration {
    23. // Spring Boot的配置文件中已经做了映射,注入了属性值
    24. private final HttpEncodingProperties properties;
    25. // 只有一个有参构造器的情况下,参数的值就会从容器中拿
    26. public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
    27. this.properties = properties;
    28. }
    29. // 给容器中添加一个组件,这个组件的某些值需要从properties中获取
    30. @Bean
    31. // 判断容器没有CharacterEncodingFilter这个组件
    32. @ConditionalOnMissingBean(CharacterEncodingFilter.class)
    33. public CharacterEncodingFilter characterEncodingFilter() {
    34. CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
    35. filter.setEncoding(this.properties.getCharset().name());
    36. filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
    37. filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
    38. return filter;
    39. }

    HttpEncodingAutoConfiguration类开头的注解,就是根据当前不同的条件进行判断,已决定这个配置类或其内部方法是否生效。一但这个配置类生效,这个配置类就会给容器中添加各种组件,这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的。

    四、properties属性类

    所有在配置文件中能配置的属性都是在xxxxProperties类中封装着,配置文件能配置什么就可以参照某个功能对应的这个属性类。

    1. // 从配置文件中获取指定的值和bean的属性进行绑定
    2. @ConfigurationProperties(prefix = "spring.http.encoding")
    3. public class HttpEncodingProperties {
    4. public static final Charset DEFAULT_CHARSET;
    5. private Charset charset;