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>
  • 无需关注版本号,自动版本仲裁 ```xml 1、引入依赖默认都可以不写版本,比如说:引入mysql驱动 mysql mysql-connector-java

2、引入非版本仲裁的jar,要写版本号。

  1. - **可以修改默认版本号**
  2. ```xml
  3. 1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
  4. 2、在当前项目里面重写配置
  5. <properties>
  6. <mysql.version>5.1.43</mysql.version>
  7. </properties>

1.2、自动配置

  • 自动配置好Tomcat
    • 引入Tomcat依赖。
    • 配置Tomcat ```xml org.springframework.boot spring-boot-starter-web

web依赖为我们引入了tomcat依赖 org.springframework.boot spring-boot-starter-tomcat 2.3.4.RELEASE compile

  1. - **自动配置好SpringMvc**
  2. - 引入SpringMVC全套组件
  3. - 自动配好SpringMVC常用组件(功能)
  4. - **自动配置好Web常见功能,如:字符编码问题**
  5. - SpringBoot帮我们配置好了所有web开发的常见场景
  6. ```java
  7. @SpringBootApplication
  8. public class MainApplication {
  9. public static void main(String[] args) {
  10. //1.返回给我们IOC容器
  11. ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
  12. //2.查看容器里面的组件
  13. String[] names = run.getBeanDefinitionNames();
  14. for (String name : names) {
  15. System.out.println("组件名称:"+name);
  16. }
  17. }
  18. }
  19. 打印内容
  20. 组件名称:dispatcherServlet
  21. 组件名称:multipartResolver
  22. 组件名称:characterEncodingFilter
  23. 组件名称:restTemplateBuilder
  24. .......
  • 默认的包结构

    • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
    • 无需以前的包扫描配置
    • 想要改变扫描路径,@SpringBootApplication(scanBasePackages=“com.hzz”)
      • 或者@ComponentScan 指定扫描路径
        1. @SpringBootApplication
        2. 等同于
        3. @SpringBootConfiguration
        4. @EnableAutoConfiguration
        5. @ComponentScan("com.atguigu.boot")
  • 各种配置的默认值

    • 默认配置最终都是映射到某个类上,如:MultipartProperties
    • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
      1. 在配置文件中修改各种配置的默认值
      2. spring.servlet.multipart.max-file-size=10Mb
  • 按需加载所有的自动配置项

    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面
    • 只有引入了自动依赖的相关配置,依赖才能够生效,否则相关的bean是爆红状态 ```xml 依赖管理 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE

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

为我们引入各种依赖,自动配置的依赖

org.springframework.boot spring-boot-autoconfigure 2.1.8.RELEASE

  1. - **......**
  2. <a name="EBGKc"></a>
  3. # 2、容器功能
  4. <a name="G6Oy7"></a>
  5. ## 2.1、组件添加
  6. <a name="oHrLy"></a>
  7. ### 1、@Configuration
  8. - 基本使用
  9. - **Full模式与Lite模式**
  10. - 示例
  11. - 最佳实战
  12. - 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
  13. - 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
  14. ```java
  15. =================================@Configuration注解使用实例============================
  16. /**
  17. * 1.配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
  18. * 2.配置类本身也是组件
  19. * 3.proxyBeanMethods:代理bean的方法
  20. * Full(proxyBeanMethods = true)、【保证每个@Bean方法被调用多少次返回的组件都是单实例的】
  21. * Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】
  22. * 组件依赖必须使用Full模式默认。其他默认是否Lite模式
  23. */
  24. @Configuration(proxyBeanMethods=true) //告诉SpringBoot这是一个配置类 == xml配置文件
  25. public class MyConfig {
  26. /**
  27. * Full:外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
  28. */
  29. @Bean //向容器中添加塑件,以方法名作为组件的id,返回类型就是组件的类型,返回的值,就是组件在容器中的实例
  30. public Person person01() {
  31. Person person = new Person("zhangsan", 10);
  32. //person组件依赖了Pet组件
  33. person.setPet(tomcatPet());
  34. return person;
  35. }
  36. @Bean("tom")
  37. public Pet tomcatPet() {
  38. return new Pet("tomcat");
  39. }
  40. }
  41. ==============================================测试代码====================================
  42. /**
  43. * 主程序类 @SpringBootApplication:这是一个SpringBoot应用
  44. */
  45. @SpringBootApplication
  46. public class MainApplication {
  47. public static void main(String[] args) {
  48. //1.返回给我们IOC容器
  49. ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
  50. //2.查看容器里面的组件
  51. String[] names = run.getBeanDefinitionNames();
  52. for (String name : names) {
  53. System.out.println("组件名称:" + name);
  54. }
  55. //3.从容器中获取组件
  56. Pet pet01 = run.getBean("tom", Pet.class);
  57. Pet pet02 = run.getBean("tom", Pet.class);
  58. System.out.println(pet01 == pet02);
  59. //4.MyConfig:com.hzz.boot.bean.config.MyConfig$$EnhancerBySpringCGLIB$$3cc8b1d0@2392212b
  60. MyConfig bean = run.getBean(MyConfig.class);
  61. System.out.println("MyConfig:"+bean);
  62. //5.如果@Configuration(proxyBeanMethods=true) 代理对象调用方法,springboot总会检查这个组件是否在容器中
  63. //保持组件的单实例
  64. //如果@Configuration(proxyBeanMethods=false) 则组件不是单实例的
  65. Person person = bean.person01();
  66. Person person1 = bean.person01();
  67. System.out.println(person == person1);
  68. //6.组件依赖,ture可以进行组件依赖,false则需重新创建
  69. Person person01 = run.getBean("person01", Person.class);
  70. Pet tomcat = run.getBean("tom", Pet.class);
  71. System.out.println("用户的宠物"+(person01.getPet() == tomcat));
  72. }
  73. }

2、@Bean、@Component、@Controller、@Service、@Repository

3、@ComponentScan、@Import

  1. * 4.@Import({User.class, DBHelper.class})
  2. * 给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
  3. */
  4. @Import({Person.class, DBHelper.class})
  5. @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
  6. public class MyConfig {
  7. }
  8. =======================测试代码=======================
  9. //7.获取组件
  10. String[] beanNamesForType = run.getBeanNamesForType(Person.class);
  11. for (String s : beanNamesForType) {
  12. System.out.println(s);
  13. }
  14. String[] namesForType = run.getBeanNamesForType(DBHelper.class);
  15. for (String name : namesForType) {
  16. System.out.println(name);
  17. }

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

4、@Conditional

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

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

2.2、原生配置文件引入

1、@ImportResource

要想原生的配置文件生效,需要使用@ImportResource注解

  1. ==================原生配置文件============================
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="haha" class="com.hzz.boot.bean.Person">
  7. <property name="username" value="zhangsan"></property>
  8. <property name="age" value="18"></property>
  9. </bean>
  10. <bean id="hehe" class="com.hzz.boot.bean.Pet">
  11. <property name="name" value="tomcat"></property>
  12. </bean>
  13. </beans>
  14. ===================如何使用注解============================
  15. @ImportResource("classpath:beans.xml") //原生配置文件的导入
  16. public class MyConfig {
  17. }
  18. =====================测试代码==============================
  19. //原生配置文件的导入hehe
  20. boolean haha = run.containsBean("haha");
  21. boolean hehe = run.containsBean("hehe");
  22. System.out.println("haha{ }" + haha);
  23. System.out.println("hehe{ }" + hehe);
  24. 打印结果
  25. haha{ }true
  26. hehe{ }true

2.2、配置绑定

如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;

1、@ConfigurationProperties

2、@EnableConfigurationProperties + @ConfigurationProperties

  1. //作用:将一个固定的配置信息封装到一个javabean中,并将配置信息放到配置文件中进行配置
  2. @EnableConfigurationProperties(Car.class)
  3. //1、开启Car配置绑定功能
  4. //2、把这个Car这个组件自动注册到容器中
  5. public class MyConfig {
  6. }
  7. /**
  8. * 只有容器中的组件,才会用于springboot的强大功能
  9. */
  10. @ConfigurationProperties(prefix = "mycar")
  11. public class Car {
  12. private String brand;
  13. private Integer price;
  14. }
  15. ===================配置文件中配置信息=======================
  16. mycar.brand=BYD
  17. mycar.price=10000

3、@Component + @ConfigurationProperties

  1. /**
  2. * 只有容器中的组件,才会用于springboot的强大功能
  3. */
  4. @Component
  5. @ConfigurationProperties(prefix = "mycar")
  6. public class Car {
  7. private String brand;
  8. private Integer price;
  9. public String getBrand() {
  10. return brand;
  11. }
  12. public void setBrand(String brand) {
  13. this.brand = brand;
  14. }
  15. public Integer getPrice() {
  16. return price;
  17. }
  18. public void setPrice(Integer price) {
  19. this.price = price;
  20. }
  21. @Override
  22. public String toString() {
  23. return "Car{" +
  24. "brand='" + brand + '\'' +
  25. ", price=" + price +
  26. '}';
  27. }
  28. }
  29. ===================测试代码====================
  30. @RestController
  31. public class HelloController {
  32. @Autowired
  33. Car car;
  34. @RequestMapping("/car")
  35. public Car hello2(){
  36. return car;
  37. }
  38. }
  39. ===================配置文件中配置信息=======================
  40. mycar.brand=BYD
  41. mycar.price=10000

3、自动配置原理入门

3.1、引导加载自动配置类

  1. =========================主启动类==========================
  2. /**
  3. * 主程序类 @SpringBootApplication:这是一个SpringBoot应用
  4. */
  5. @SpringBootApplication
  6. public class MainApplication {
  7. public static void main(String[] args) {
  8. }
  9. }
  10. ===============进入@SpringBootApplication注解中========================
  11. @SpringBootConfiguration
  12. @EnableAutoConfiguration
  13. @ComponentScan
  14. public @interface SpringBootApplication {
  15. }

1、@SpringBootConfiguration

@Configuration。代表当前是一个配置类,配置一些spring的配置信息,实现配置文件的功能,相当于spring的一个xml配置文件;

2、@ComponentScan

进行spring的组件扫描,主要扫描相关包下符合规则的类,主要是以下注解:@Controller @Mapper @Service @Compent,首先根据配置和注解定位到Bean,然后将信息保存到BeanDefinition中,然后将BeanDefinition发布到IOC容器中,此时Bean还没有初始化,spring有一个lazy-init的配置,默认是false,不会延时初始化,如果设置为true则会在使用的时候才进行初始化。初始化后需要经过注入,一般会用@Aw…或者@Rec…进行注入;

3、@EnableAutoConfiguration

  1. //自动配置包,指定了默认的包规则
  2. @AutoConfigurationPackage
  3. //给容器中导入一个该类的组件、默认组件的名字就是全类名
  4. @Import({AutoConfigurationImportSelector.class})

1.@AutoConfigurationPackage

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

  1. @Import({Registrar.class}) //给容器中导入一个该类的组件、默认组件的名字就是全类名
  2. public @interface AutoConfigurationPackage {
  3. }
  4. //利用Registrar给容器中导入一系列组件
  5. //将指定的一个包下的所有组件导入进来?MainApplication 所在包下。

2.@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. 文件里面写死了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、按需开启自动配置项

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

3.3、修改默认配置

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

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

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public CharacterEncodingFilter characterEncodingFilter() {
  4. }

总结:

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

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