• SpringBoot学习(六)-->SpringBoot的自动配置的原理


    Spring Boot的自动配置的原理

    Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器。

    1、Maven下载源码

    通过 dependency:sources 该命令可以下载该项目中所有的依赖的包的源码。

    2、源码分析

    在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->META-INF-->spring.factories

     1 # PropertySource Loaders
     2 org.springframework.boot.env.PropertySourceLoader=
     3 org.springframework.boot.env.PropertiesPropertySourceLoader,
     4 org.springframework.boot.env.YamlPropertySourceLoader
     5 
     6 # Run Listeners
     7 org.springframework.boot.SpringApplicationRunListener=
     8 org.springframework.boot.context.event.EventPublishingRunListener
     9 
    10 # Application Context Initializers
    11 org.springframework.context.ApplicationContextInitializer=
    12 org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,
    13 org.springframework.boot.context.ContextIdApplicationContextInitializer,
    14 org.springframework.boot.context.config.DelegatingApplicationContextInitializer,
    15 org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer
    16 
    17 # Application Listeners
    18 org.springframework.context.ApplicationListener=
    19 org.springframework.boot.ClearCachesApplicationListener,
    20 org.springframework.boot.builder.ParentContextCloserApplicationListener,
    21 org.springframework.boot.context.FileEncodingApplicationListener,
    22 org.springframework.boot.context.config.AnsiOutputApplicationListener,
    23 org.springframework.boot.context.config.ConfigFileApplicationListener,
    24 org.springframework.boot.context.config.DelegatingApplicationListener,
    25 org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,
    26 org.springframework.boot.logging.ClasspathLoggingApplicationListener,
    27 org.springframework.boot.logging.LoggingApplicationListener
    28 
    29 # Environment Post Processors
    30 org.springframework.boot.env.EnvironmentPostProcessor=
    31 org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,
    32 org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
    33 
    34 # Failure Analyzers
    35 org.springframework.boot.diagnostics.FailureAnalyzer=
    36 org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,
    37 org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,
    38 org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,
    39 org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,
    40 org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,
    41 org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,
    42 org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer
    43 
    44 # FailureAnalysisReporters
    45 org.springframework.boot.diagnostics.FailureAnalysisReporter=
    46 org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
    spring.factories

    其中配置了很多内容,如第11行的ApplicationContextInitializer;接下来我们详细了解一下SpringApplication

    在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.springframework.boot-->SpringApplication.class:


     

    按住ctrl点击loadFactoryNames:

    能在org.springframework.core.io.support下找到SpringFactoriesLoader类:

    所以其是读取该配置文件来加载内容的。

    3、举例:Redis的自动配置

    从上述的配置中可以看出,在Maven Dependencies-->spring-boot-autoconfigure-1.5.2.RELEASE.jar-->org.springframework.boot.autoconfigure.data.redis-->RedisAutoConfiguration.class是Redis的自动配置。


     

    4、源码中的条件注解的解释

    @ConditionalOnBean: 当容器里有指定的Bean的条件下
    @ConditionalOnClass: 当类路径下有指定的类的条件下
    @ConditionalOnExpression: 基于SpEL表达式作为判断条件
    @ConditionalOnJava: 基于JVM版本作为判断条件
    @ConditionalOnJndi: 在JNDI存在的条件下查找指定的位置
    @ConditionalOnMissingBean: 当容器里没有指定Bean的情况下
    @ConditionalOnMissingClass:   当类路径下没有指定的类的条件下
    @ConditionalOnNotWebApplication: 当前项目不是Web项目的条件下
    @ConditionalOnProperty: 指定的属性是否有指定的值
    @ConditionalOnResource: 类路径是否具有指定的值
    @ConditionalOnSingleCandidate: 当指定Bean在容器中只有一个,或者虽然有多个但是指定首选的Bean
    @ConditionalOnWebApplication: 当前项目是Web项目的条件下

    到这里,SpringBoot的概念原理实现之类的了解的差不多了,接下来我们开始将SpringBoot整合到真正的项目中。

  • 相关阅读:
    使用Random类生成指定范围的随机数
    js ==和===的区别
    js中的undefined
    js的split函数
    springboot发送邮件
    1032 Sharing (25分)(数组链表)
    1031 Hello World for U (20分)
    1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
    1029 Median (25分)
    1026 Table Tennis (30分)
  • 原文地址:https://www.cnblogs.com/mmzs/p/9257451.html
Copyright © 2020-2023  润新知