SpringBoot-自动配置原理(七)

自动配置原理

本节内容分为三个部分java

  • 配置文件的写法
  • 分析自动配置原理
  • @Conditional

一. 配置文件的写法

配置文件能够写什么?web

是与/META-INF/spring.factories配置文件相关联,在该文件中,咱们经过源码能够找到面试

咱们在配置文件中要写的配置spring

二 .分析自动配置原理

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

2.@EnableAutoConfiguration 做用 :mvc

  • 利用EnableAutoConfigurationImportSelector给容器中导入一些组件,导入了哪些组件呢?app

  • 能够查看这个类selectImports()方法的内容,他返回了一个 autoConfigurationEntry , 来自 this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata); 这个方法。咱们继续跟踪;this

  • 这个方法中有一个值 : List configurations = this.getCandidateConfigurations(annotationMetadata, attributes); 叫作获取候选的配置 , 咱们点击去继续跟踪; 编码

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

    这里里面有一个 SpringFactoriesLoader.loadFactoryNames() ,咱们继续进去看 , 它又调用了 loadSpringFactories 方法;继续跟踪。发现它去得到了一个资源文件:"META-INF/spring.factories"url

    Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");

    继续阅读源码 , 它将读取到的资源封装在url中,而后遍历url , 将这些url文件封装在Properties文件中;最后返回封装好的结果;他的那个ClassLoader参数,咱们追踪回去,看到他就是 EnableAutoConfiguration ;

  • 说明了这个逻辑就是 从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,而后把他们添加在容器中

  • 总结一句话就是:将类路径下 META-INF/spring.factories 里面配置的全部EnableAutoConfiguration的值加入到了容器中;咱们从源码中拿过来

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

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

4.咱们以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理;

@Configuration //表示这是一个配置类,之前编写的配置文件同样,也能够给容器中添加组件

//启动指定类的ConfigurationProperties功能;
//进入这个HttpProperties查看,将配置文件中对应的值和HttpProperties绑定起来;
//并把HttpProperties加入到ioc容器中
@EnableConfigurationProperties({HttpProperties.class}) 

//Spring底层@Conditional注解
//根据不一样的条件判断,若是知足指定的条件,整个配置类里面的配置就会生效;
//这里的意思就是判断当前应用是不是web应用,若是是,当前配置类生效
@ConditionalOnWebApplication(
    type = Type.SERVLET
)

//判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器;
@ConditionalOnClass({CharacterEncodingFilter.class})

//判断配置文件中是否存在某个配置:spring.http.encoding.enabled;
//若是不存在,判断也是成立的
//即便咱们配置文件中不配置spring.http.encoding.enabled=true,也是默认生效的;
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)

public class HttpEncodingAutoConfiguration {

    //他已经和SpringBoot的配置文件映射了
    private final Encoding properties;

    //只有一个有参构造器的状况下,参数的值就会从容器中拿
    public HttpEncodingAutoConfiguration(HttpProperties properties) {
        this.properties = properties.getEncoding();
    }

    //给容器中添加一个组件,这个组件的某些值须要从properties中获取
    @Bean
    @ConditionalOnMissingBean //判断容器没有这个组件?
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
        return filter;
    }
    。。。。。。
}

一句话总结 : 根据当前不一样的条件判断,决定这个配置类是否生效!

一但这个配置类生效;这个配置类就会给容器中添加各类组件;这些组件的属性是从对应的properties类中获取的,这些类里面的每个属性又是和配置文件绑定的;

5.全部在配置文件中能配置的属性都是在xxxxProperties类中封装者‘;配置文件能配置什么就能够参照某个功能对应的这个属性类

@ConfigurationProperties(
    prefix = "spring.http"
) //从配置文件中获取指定的值和bean的属性进行绑定
public class HttpProperties {
    private boolean logRequestDetails;
    private final HttpProperties.Encoding encoding = new HttpProperties.Encoding();

    public HttpProperties() {
    }

    public boolean isLogRequestDetails() {
        return this.logRequestDetails;
    }

    public void setLogRequestDetails(boolean logRequestDetails) {
        this.logRequestDetails = logRequestDetails;
    }

    public HttpProperties.Encoding getEncoding() {
        return this.encoding;
    }

    public static class Encoding {
        public static final Charset DEFAULT_CHARSET;
        private Charset charset;
        private Boolean force;
        private Boolean forceRequest;
        private Boolean forceResponse;
        private Map<Locale, Charset> mapping;
        
        、、、、、、
    }
}

咱们去配置文件里面试试前缀,看提示!

这就是自动装配的原理!

精髓:

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

2)、咱们看咱们须要的功能有没有在SpringBoot默认写好的自动配置类当中;

3)、咱们再来看这个自动配置类中到底配置了哪些组件;(只要咱们要用的组件存在在其中,咱们就不须要再手动配置了)

4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。咱们只须要在配置文件中指定这些属性的值便可;

xxxxAutoConfigurartion:自动配置类;向容器中添加组件

xxxxProperties:封装配置文件中相关属性;修改配置,就是咱们的application.yml(properties)配置文件

@ConditionalXxx的存在,会屏蔽组件,必须符合必定的条件才生效

例如:spring.mvc的属性配置

全部能配置的属性都在这里

或者咱们在META-INF/spring.factories文件下找到Mvc相关的

三 . @Conditional

了解完自动装配的原理后,咱们来关注一个细节问题 ,自动配置类必须在必定的条件下才能生效;

@Conditional派生注解(Spring注解版原生的@Conditional做用)

做用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的全部内容才生效;

那么多的自动配置类,必须在必定的条件下才能生效;也就是说,咱们加载了这么多的配置类,但不是全部的都生效了。

咱们怎么知道哪些自动配置类生效;咱们能够经过启用 debug=true属性;来让控制台打印自动配置报告,这样咱们就能够很方便的知道哪些自动配置类生效;

#开启springboot的调试类
debug=true

Positive matches:(自动配置类启用的:正匹配)

Negative matches:(没有启动,没有匹配成功的自动配置类:负匹配)

Unconditional classes: (没有条件的类)

输出的日志咱们能够在这里看下:控制台打印日志

相关文章
相关标签/搜索