• 尊龙人生就是博

SpringBoot自动装配原理分析

关键词:SpringBoot,自动,装配,原理,分析,SpringB

SpringBoot能通走首来吾觉得有个重要的因为就是它的自动装配,想想操纵SpringBoot与Spring开发项方针不同,第一逆答就是缩短了许众配置。 其实SpringBoot是基于Spring基础之上的,议定MAVEN引

  • SpringBoot能通走首来吾觉得有个重要的因为就是它的自动装配,想想操纵SpringBoot与Spring开发项方针不同,第一逆答就是缩短了许众配置。

    其实SpringBoot是基于Spring基础之上的,议定MAVEN引入SpringBoot也会发现,它传递倚赖了Spring。于是配置并异国缩短,只是它帮吾们自动配置了,这些自动配置的类都在spring-boot-autoconfigure-版本.jar包中。

    以一个最轻易的SpringBoot行使为例,清淡必要写云云一个启动类:

    进入@SpringBootApplication注明内部,能够望到与自动装配相关的@EnableAutoConfiguration:

    打开盈余90%

    @EnableAutoConfiguration议定@import手段引入了EnableAutoConfigurationImportSelector:

    EnableAutoConfigurationImportSelector是一个ImportSelector, 下载918博天堂ag厅吾们查望其selectImports手段,其内部调用getCandidateConfigurations手段,读取META-INF/spring.factories里配置的Configuration类。

    @Override

    public String[] selectImports(AnnotationMetadata annotationMetadata) {

    if (!isEnabled(annotationMetadata)) {

    return NO_IMPORTS;

    }

    try {

    AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);

    AnnotationAttributes attributes = getAttributes(annotationMetadata);

    Listconfigurations = getCandidateConfigurations(annotationMetadata, attributes); // META-INF/spring.factories

    configurations = removeDuplicates(configurations);

    configurations = sort(configurations, autoConfigurationMetadata);

    Setexclusions = getExclusions(annotationMetadata, attributes);

    checkExcludedClasses(configurations,尊龙现金人生就是博 exclusions);

    configurations.removeAll(exclusions);

    configurations = filter(configurations, autoConfigurationMetadata);

    fireAutoConfigurationImportEvents(configurations, exclusions);

    return configurations.toArray(new String[configurations.size()]);

    }

    catch (IOException ex) {

    throw new IllegalStateException(ex);

    }

    }

    在SpringBoot的spring-boot-autoconfigure-版本.jar包中,spring.factories文件里预定义了许众Configuration类:

    # Initializers

    org.springframework.context.ApplicationContextInitializer=\

    org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\

    org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer

    # Application Listeners

    org.springframework.context.ApplicationListener=\

    org.springframework.boot.autoconfigure.BackgroundPreinitializer

    # Auto Configuration Import Listeners

    org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\

    org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

    # Auto Configuration Import Filters

    org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\

    org.springframework.boot.autoconfigure.condition.OnClassCondition

    # 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,\

    org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\

    ......

    到这边自动装配的主流程就终结了,然后详细的装配逻辑就在各自的Configuration类中。

    以常见的HttpEncodingAutoConfiguration为例,这些Configuration类都添补了@Configuration注明外名都是配置类,然后议定例如:@ConditionalXXX等判定议定代码手段创建必要用到的Bean。

    @Configuration

    @EnableConfigurationProperties(HttpEncodingProperties.class)

    @ConditionalOnWebApplication

    @ConditionalOnClass(CharacterEncodingFilter.class)

    @ConditionalOnProperty(prefix = spring.http.encoding, value = enabled, matchIfMissing = true)

    public class HttpEncodingAutoConfiguration {

    private final HttpEncodingProperties properties;

    public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {

    this.properties = properties;

    }

    @Bean

    @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;

    }

    @Bean

    public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() {

    return new LocaleCharsetMappingsCustomizer(this.properties);

    }

    private static class LocaleCharsetMappingsCustomizer implements EmbeddedServletContainerCustomizer, Ordered {

    private final HttpEncodingProperties properties;

    LocaleCharsetMappingsCustomizer(HttpEncodingProperties properties) {

    this.properties = properties;

    }

    @Override

    public void customize(ConfigurableEmbeddedServletContainer container) {

    if (this.properties.getMapping() != null) {

    container.setLocaleCharsetMappings(this.properties.getMapping());

    }

    }

    @Override

    public int getOrder() {

    return 0;

    }

    }

    }

    ,,
发表时间:2019-08-30 | 评论 () | 复制本页地址 | 打印

相关文章