1、SpringBoot特点

1.1、依赖管理

  • 父项目做依赖管理 ```xml 依赖管理 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE

他的父项目 org.springframework.boot spring-boot-dependencies 2.3.4.RELEASE

几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制

  1. - 开发导入starter场景启动器
  2. ```xml
  3. 1、见到很多 spring-boot-starter-* : *就某种场景
  4. 2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
  5. 3、SpringBoot所有支持的场景
  6. https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
  7. 4、见到的 *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
  8. 5、所有场景启动器最底层的依赖
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter</artifactId>
  12. <version>2.3.4.RELEASE</version>
  13. <scope>compile</scope>
  14. </dependency>
  • 无需关注版本号,自动版本仲裁

引入依赖默认都可以不写版本
引入非版本仲裁的jar,要写版本号。

  • 可以修改默认版本号

    1. 1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
    2. 2、在当前项目里面重写配置
    3. <properties>
    4. <mysql.version>5.1.43</mysql.version>
    5. </properties>

    1.2、自动配置

  • 自动配好Tomcat

    • 引入Tomcat依赖。
    • 配置Tomcat
      1. <dependency>
      2. <groupId>org.springframework.boot</groupId>
      3. <artifactId>spring-boot-starter-tomcat</artifactId>
      4. <version>2.3.4.RELEASE</version>
      5. <scope>compile</scope>
      6. </dependency>
      这边的话其实在依赖管理引入web场景的时候,就已经引入了Tomcat场景。无需再配置
  • 自动配好SpringMVC

    • 引入SpringMVC全套组件
    • 自动配好SpringMVC常用组件(功能)
  • 自动配好Web常见功能,如:字符编码问题
    • SpringBoot帮我们配置好了所有web开发的常见场景
  • 默认的包结构
    • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
    • 无需以前的包扫描配置
    • 想要改变扫描路径,@SpringBootApplication(scanBasePackages=**"com.atguigu"**)
      • 或者@ComponentScan 指定扫描路径
  1. @SpringBootApplication
  2. 等同于 ==
  3. @SpringBootConfiguration
  4. @EnableAutoConfiguration
  5. @ComponentScan("com.atguigu.boot")
  • 各种配置拥有默认值
    • 默认配置最终都是映射到某个类上,如:MultipartProperties
    • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
  • 按需加载所有自动配置项
    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面

其实所有自动配置都依赖@EnableAutoConfiguration

2、容器功能

2.1、组件添加

1、@Configuration

  • 基本使用
  • Full模式与Lite模式

    • 示例
    • 最佳实战

      • 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
      • 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式 ```java
        #######################Configuration使用示例
        /**
        • 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
        • 2、配置类本身也是组件
        • 3、proxyBeanMethods:代理bean的方法
        • Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
        • Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
        • 组件依赖必须使用Full模式默认。其他默认是否Lite模式 / @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件 public class MyConfig { /*
        • Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
        • @return */ @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例 public User user01(){ User zhangsan = new User(“zhangsan”, 18); //user组件依赖了Pet组件 zhangsan.setPet(tomcatPet()); return zhangsan; }

      @Bean(“tom”) public Pet tomcatPet(){ return new Pet(“tomcat”); } }

##########################@Configuration测试代码如下

@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(“com.atguigu.boot”) public class MainApplication {

  1. public static void main(String[] args) {
  2. //1、返回我们IOC容器
  3. ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
  4. //2、查看容器里面的组件
  5. String[] names = run.getBeanDefinitionNames();
  6. for (String name : names) {
  7. System.out.println(name);
  8. }
  9. //3、从容器中获取组件
  10. Pet tom01 = run.getBean("tom", Pet.class);
  11. Pet tom02 = run.getBean("tom", Pet.class);
  12. System.out.println("组件:"+(tom01 == tom02));
  13. //4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892
  14. MyConfig bean = run.getBean(MyConfig.class);
  15. System.out.println(bean);
  16. //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
  17. //保持组件单实例
  18. User user = bean.user01();
  19. User user1 = bean.user01();
  20. System.out.println(user == user1);
  21. User user01 = run.getBean("user01", User.class);
  22. Pet tom = run.getBean("tom", Pet.class);
  23. System.out.println("用户的宠物:"+(user01.getPet() == tom));
  24. }

}

  1. <a name="0tCTs"></a>
  2. ### 2、@Bean、@Component、@Controller、@Service、@Repository
  3. 根据它们的源码可以看到,Controller、Service、Repository其本质就是Component。<br />它存在的本质只是给开发者看的,对Spring而言它们就都是Component。<br />@Controller 控制层类,@Service 业务层类,@Repository 持久层类,@Component 无法归类到前3种时就称为组件。
  4. <a name="ynNRo"></a>
  5. ### 3、@ComponentScan、@Import
  6. ```java
  7. * 4、@Import({User.class, DBHelper.class})
  8. * 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名,需要在组件类上标注
  9. 作用和component一样,也是给容器注入组件
  10. */
  11. @Import({User.class, DBHelper.class})
  12. @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
  13. public class MyConfig {
  14. }

@Import 高级用法: https://www.bilibili.com/video/BV1gW411W7wy?p=8

4、@Conditional

条件装配:满足Conditional指定的条件,则进行组件注入
1、SpringBoot自动配置原理 - 图1

  1. =====================测试条件装配==========================
  2. @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
  3. //@ConditionalOnBean(name = "tom")
  4. @ConditionalOnMissingBean(name = "tom")
  5. public class MyConfig {
  6. /**
  7. * Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
  8. * @return
  9. */
  10. @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
  11. public User user01(){
  12. User zhangsan = new User("zhangsan", 18);
  13. //user组件依赖了Pet组件
  14. zhangsan.setPet(tomcatPet());
  15. return zhangsan;
  16. }
  17. @Bean("tom22")
  18. public Pet tomcatPet(){
  19. return new Pet("tomcat");
  20. }
  21. }
  22. public static void main(String[] args) {
  23. //1、返回我们IOC容器
  24. ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
  25. //2、查看容器里面的组件
  26. String[] names = run.getBeanDefinitionNames();
  27. for (String name : names) {
  28. System.out.println(name);
  29. }
  30. boolean tom = run.containsBean("tom");
  31. System.out.println("容器中Tom组件:"+tom);
  32. boolean user01 = run.containsBean("user01");
  33. System.out.println("容器中user01组件:"+user01);
  34. boolean tom22 = run.containsBean("tom22");
  35. System.out.println("容器中tom22组件:"+tom22);
  36. }

3、自动配置原理入门

3.1、引导加载自动配置类

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  4. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  5. public @interface SpringBootApplication{}
  6. ======================

上面三个注解相当于@SpringBootApplication

@SpringBootConfiguration

@Configuration。代表当前是一个配置类

@ComponentScan

指定扫描哪些,Spring注解;

@EnableAutoConfiguration

  1. @AutoConfigurationPackage
  2. @Import(AutoConfigurationImportSelector.class)
  3. public @interface EnableAutoConfiguration {}

① @AutoConfigurationPackage

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

  1. @Import(AutoConfigurationPackages.Registrar.class) //给容器中导入一个组件
  2. public @interface AutoConfigurationPackage {}
  3. //利用Registrar给容器中导入一系列组件
  4. //将指定的一个包下的所有组件导入进来?MainApplication 所在包下。

②Import(AutoConfigurationImportSelector.class)

  1. 1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
  2. 2、调用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
  3. 3、利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件
  4. 4、从META-INF/spring.factories位置来加载一个文件。
  5. 默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件
  6. spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories

1、SpringBoot自动配置原理 - 图2
自带127个配置类

  1. 文件里面写死了spring-boot一启动就要给容器中加载的所有配置类
  2. spring-boot-autoconfigure-2.3.4.RELEASE.jar/META-INF/spring.factories
  3. # Auto Configure
  4. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  5. org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
  6. org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  7. org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
  8. org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
  9. org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
  10. org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
  11. org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
  12. org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
  13. org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
  14. org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
  15. org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
  16. org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
  17. org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
  18. org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
  19. org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
  20. org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
  21. org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
  22. org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
  23. org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
  24. org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
  25. org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
  26. org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
  27. org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\
  28. org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\
  29. org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
  30. org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
  31. org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
  32. org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
  33. org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
  34. org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
  35. org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
  36. org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
  37. org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
  38. org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
  39. org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\
  40. org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\
  41. org.springframework.boot.autoconfigure.data.r2dbc.R2dbcTransactionManagerAutoConfiguration,\
  42. org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
  43. org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
  44. org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
  45. org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
  46. org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
  47. org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration,\
  48. org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
  49. org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
  50. org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
  51. org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
  52. org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
  53. org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
  54. org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
  55. org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
  56. org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
  57. org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
  58. org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
  59. org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
  60. org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
  61. org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
  62. org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
  63. org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
  64. org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
  65. org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
  66. org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
  67. org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
  68. org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
  69. org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
  70. org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
  71. org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
  72. org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
  73. org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
  74. org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
  75. org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
  76. org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\
  77. org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
  78. org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
  79. org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
  80. org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
  81. org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
  82. org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
  83. org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
  84. org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
  85. org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
  86. org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
  87. org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
  88. org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
  89. org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\
  90. org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\
  91. org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\
  92. org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\
  93. org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
  94. org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
  95. org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
  96. org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
  97. org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
  98. org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
  99. org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
  100. org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
  101. org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
  102. org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
  103. org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
  104. org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
  105. org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
  106. org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
  107. org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
  108. org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
  109. org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
  110. org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
  111. org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
  112. org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
  113. org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
  114. org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
  115. org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
  116. org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
  117. org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
  118. org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
  119. org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
  120. org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
  121. org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
  122. org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
  123. org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
  124. org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
  125. org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
  126. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
  127. org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
  128. org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
  129. org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
  130. org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
  131. org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

3.2、按需开启自动配置项

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

3.3、修改默认配置

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

给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找,@Autowrited也是

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

  1. @Bean
  2. @ConditionalOnMissingBean //用户没有配置的情况下我帮你配
  3. public CharacterEncodingFilter characterEncodingFilter() {
  4. }

总结:

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

xxxxxAutoConfiguration —-> 组件 —-> xxxxProperties里面拿值 ——> application.properties

3.4、最佳实践