SpringBoot自动配置原理

SpringBoot帮我们简化了大量繁琐的代码以及许多工具类,它的自动配置是一个比较好用的东西,今天就学习了一下SpringBoot的自动配置原理,现在总结一下:
1、自动配置原理:
a.SpringBoot启动的时候会加载主配置类,然后开启自动配置功能:@EnableAutoConfiguration 在这里插入图片描述
b.@EnableAutoConfiguration的作用是是什么?我总结了一下:
b1.可以利用用EnableAutoConfigurationImportSelector给容器中导入一下组件在这里插入图片描述
b2.可以查看selectImports()方法的内容在这里插入图片描述
b3.List configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候 选的配置
获取候选的参数配置的核心步骤:
SpringFactoriesLoader.loadFactoryNames()在这里插入图片描述
扫描所有jar包类路径下 META‐INF/spring.factories 把扫描到的这些文件的内容包装成properties对象 在这里插入图片描述
从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中 在这里插入图片描述
配置“之源”就是将类路径下META-INF/spring.factories里面的所有EnableAutoConfiguration的值加入到了容器中。
如下所有的xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中,用来配置
在这里插入图片描述

之后进行每一个自动类进行自动配置功能:(以HttpEncodingAutoConfiguration举例)
	@Configuration   //表示这是一个配置类,也可以给容器中添加组件 
	@EnableConfigurationProperties(HttpEncodingProperties.class)  //启动指定类的ConfigrationProperties功能,将配置文件中对应的值和HttpEncodingProperties绑定起来;并把 HttpEncodingProperties加入到ioc容器中
	@ConditionalOnWebApplication//spring底层@Conditional注解,可以用来表示判断条件
	@ConditionalOnClass(CharacterEncodingFilter.class)  //判断当前项目有没有这个类 
	@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing =  true) //判断配置文件是否存在某个配置 这个是判断"spring。http.encoding"是否存在,默认是存在是为true


public class HttpEncodingAutoConfiguration {       

//他已经和SpringBoot的配置文件映射了   

   private final HttpEncodingProperties properties
 //只有一个有参构造器的情况下,参数的值就会从容器中拿    
 
	 public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {   
	 	 this.properties = properties;         
	}    
 	         
  @Bean   //给容器中添加一个组件,这个组件的某些值需要从properties中获取  
  @ConditionalOnMissingBean(CharacterEncodingFilter.class)//判断容器没有这个组件?     
 
public CharacterEncodingFilter characterEncodingFilter() {      
	CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();       
 		filter.setEncoding(this.properties.getCharset().name());         
 		filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));   
 		filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));  
 		return filter; 
}

这个根据不同的条件判断,决定这个文件是否生效,一旦生效,这个配置文件就会给容器中添加各种组件,这些组件的属性就是对应的properties类种获取的,这些类里面的每一个属性又和配置文件绑定。
如下:
所有在配置文件中能够配置的属性都是能够在XXXproperties的类种封装,配置文件需要什么功能,就可以properties中的某个属性。

@ConfigurationProperties(prefix = "spring.http.encoding")  //从配置文件中获取指定的值和					bean的属 性进行绑定 
	 	public class HttpEncodingProperties {      
	 	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF‐8");

检查是否生效:
设置debug=true的属性:在控制台打印看看

  ========================= AUTO‐CONFIGURATION REPORT =========================     Positive matches:(自动配置类启用的) ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ 
   DispatcherServletAutoConfiguration matched:
 ‐ @ConditionalOnClass found required class  
 'org.springframework.web.servlet.DispatcherServlet'; 
    @ConditionalOnMissingClass did not find  unwanted class (OnClassCondition)    
   		-  @ConditionalOnWebApplication (required) found StandardServletEnvironment  (OnWebApplicationCondition)    
 Negative matches:(没有启动,没有匹配成功的自动配置类)
  ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐     
  ActiveMQAutoConfiguration:      
                 Did not match:         
                  ‐ @ConditionalOnClass did not find required classes'javax.jms.ConnectionFactory',  'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)      AopAutoConfiguration:       
                   Did not match:        
                     ‐ @ConditionalOnClass did not find required classes  'org.aspectj.lang.annotation.Aspect','org.aspectj.lang.reflect.Advice' (OnClassCondition) 

精髓所在:

1)、SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3)、只要我们要用的组件有,我们就不需要再来配置了
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这 些属性的值;

如果大家有更好的建议,请及时提出。
总结不是很到位,推荐大家可以看看一些视频里面的总结,本人参考的是尚硅谷的视频。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值