SpringBoot自动配置原理、与配置文件的关系

1、首先找到spring-boot-autoconfigure下的spring.factories文件,因为启动SpringBoot时,它会找到这个文件,读取里面的东西
在这里插入图片描述
2.查看spring.factories,以WebMvcAutoConfiguration为例,点进去查看源码
在这里插入图片描述
继续点进去找到@ConfigurationProperties(prefix = “spring.mvc”),然后在yaml配置文件中输入spring.mvc.dis发现它会有属性的提示,所以发现它们之间是存在关系的。

在yaml这配置文件能配置的东西,都存在一个固有的规律(左边的打开的是WebMvcProperties.java)ConfigurationProperties(有这个就可以自定义 绑定配置文件,见图二)<---- xxxxAutoConfiguration:(自动装配)默认值是固定的,如果我们想修改属性,不让它走默认,只要在yaml或Prop配置它里面相对于的属性就行,实现使用yaml配置文件就可以动态修改我们springboot的默认属性
在这里插入图片描述

小结:

问:什么是组件?

组件就是Componen,相当于加了@Componen注解后,这个类交给了spring容器托管

每一个这样的xxx(Enable)ConfigurationProperties 类都是容器中的一个组件,最后都加入到spring容器中;用它们来做自动装配

总结:

  1. springBoot启动会加载大量的自动配置类
  2. 我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
  3. 我们在来看这个自动配置类中到底配置了哪些组件(只要我们要用的组件存在其中,我们就不需要手动配置了)
  4. 给容器中自动配置类添加组件的时候,会从Properties类中获取某属性,我们只需要在配置文件中指定这些属性的值即可;
    在这里插入图片描述
    点开另一个自动配置类观察,点进ServerProperties类中也可以找到类似@ConfigurationProperties(prefix = “spring.mvc”),表面它绑定了yaml配置文件,我们可以通过配置文件修改默认属性。
    在这里插入图片描述

问:SpringBoot中那么多自动配置类,什么时候生效,而什么时候不生效?

以 spring-boot-starter-webweb启动器为例
在配置文件 application.properties中输入
debug=true

然后启动SpringBoot,观察控制台打印

可以看到打印三种情况Positive matches:(生效的)、Negative matches:(没有生效的)、( 不符合条件的类)Unconditional classes

Positive matches:(生效的)
这里只截取了一小部分,可以看到这些生效的都是与Web常用的东西
如spring的aop、ClassProxy代理类配置、DispatcherServlet(SpringMVC的)

Positive matches:
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)

   DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
      - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration' (OnClassCondition)
      - Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)

   DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration matched:
      - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration' (OnClassCondition)
      - DispatcherServlet Registration did not find servlet registration bean (DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition)

   DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration matched:
      - @ConditionalOnBean (names: dispatcherServlet types: org.springframework.web.servlet.DispatcherServlet; SearchStrategy: all) found bean 'dispatcherServlet' (OnBeanCondition)

没有生效的Negative matches:

Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   BatchAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.batch.core.launch.JobLauncher' (OnClassCondition)

   CacheAutoConfiguration:
      Did not match:
         - @ConditionalOnBean (types: org.springframework.cache.interceptor.CacheAspectSupport; SearchStrategy: all) did not find any beans of type org.springframework.cache.interceptor.CacheAspectSupport (OnBeanCondition)
      Matched:
         - @ConditionalOnClass found required class 'org.springframework.cache.CacheManager' (OnClassCondition)

   CacheAutoConfiguration.CacheManagerEntityManagerFactoryDependsOnPostProcessor:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean' (OnClassCondition)


不符合条件的类Unconditional classes

Unconditional classes:
----------------------
    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration

    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

    org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration

    org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration

问:如果我想用没有生效的某个自动配置类呢?

在pox.xml导入相关依赖就行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值