spring学习笔记(十)springboot 配置

2.11 自动配置原理(二)

自动配置原理:

1.springBoot启动的时候自动加载主配置类,开启了自动配置功能@EnableAutoConfiguration

2.@EnableAutoConfiguration的作用:

  • 利用AutoConfigurationImportSelector给容器中导入一些组件
  • 可以查看父类的selectImports()方法的内容
  • List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);//获取候选的配置
  • SpringFactoriesLoader.loadFactoryNames()方法(getCandidateConfigurations点击来
SpringFactoriesLoader.loadFactoryNames
//扫描所有jar类路径下的META-INF/spring.factories
//把扫描到的文件内容包装成properties对象
while(urls.hasMoreElements()) {
      URL url = (URL)urls.nextElement();
      Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
      String factoryClassNames = properties.getProperty(factoryClassName);                            
}

//从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加到容器中
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
//第一个参数就是获取类名对应的值,第二个就是把扫描到的文件内容包装成properties对象

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

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\

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

3.每一个自动配置类进行自动配置功能

4.以HttpEncodingAutoConfiguration(http自动编码功能)为例看自动配置原理

@Configuration//表示这个一个配置类,
@EnableConfigurationProperties({HttpEncodingProperties.class})//启动指定类的ConfigurationProperties功能,将配置文件中对应的值和HttpEncodingProperties绑定起来,
@ConditionalOnWebApplication//spring底层有个@Conditional注解(spring注解版),根据不同条件,如果满足指定的条件,里面的配置才会生效,判断当前应用是都是web应用
@ConditionalOnClass({CharacterEncodingFilter.class})//判断当前项目有没有CharacterEncodingFilter这个类,springMCV中进行乱码解决的过滤器
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)//判断配置文件中是否存在某个配置;spring.http.encoding.enabled ,如果不存在也是返回true,即使我们配置文件中不配置也是默认生效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类中获取的,这个配置类中的每个属性又是和配置文件绑定的;

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

//从配置文件中获取指定的值和bean绑定
@ConfigurationProperties(
    prefix = "spring.http.encoding"
)
public class HttpEncodingProperties {

6.精髓:

1)、SpringBoot启动会加载大量的自动配置类

2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类

3)、我们再来看这个自动配置类中到底配置了哪些组件,只要我们要用的组件有,我们就不需要再来配置

4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性,我们就可以在配置文件中指定这些属性的值

xxxAutoConfiguration:自动配置类,给容器添加组件,如CacheAutoConfiguration

xxxProperties:封装配置文件中相关的属性,如CacheProperties

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值