• Spring Boot自动配置


    Spring Boot启动原理图

    Spring Boot自动装配

    自动配置第一步:@SpringBootApplication

    @SpringBootApplication
    public class TestApplication {
        public static void main(String[] args) {
            SpringApplication.run(TestApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    OK,进入@SpringBootApplication,可以看到内部注解如下:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    重要的注解作用如下:

    • @SpringBootConfiguration 加载配置类(@Configuration,使用Java Config配置方式)
    • @EnableAutoConfiguration 开启自动配置
    • @ComponentScan 组件扫描,扫描spring的bean(@Controller,@RestController,@Service,@Mapper,@Repository等等)

    由此可见,自动配置第二步:@EnableAutoConfiguration

    此注解开启自动配置

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import({AutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {
        String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    
        Class<?>[] exclude() default {};
    
        String[] excludeName() default {};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    重点在于:

    • @AutoConfigurationPackage 自动配置包
    • @Import({AutoConfigurationImportSelector.class})

    自动配置第三步:@Import({AutoConfigurationImportSelector.class})

    这一步重点在于导入,它导入了谁?

    • AutoConfigurationImportSelector.class

    其导入的AutoConfigurationImportSelectorselectImports方法通过SpringFactoriesLoader.loadFactoryNames扫描所有具有META-INF/spring.factories的jar包下面key是EnableAutoConfiguration全名的所有自动配置类。

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
            List<String> configurations = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
            ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
            Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
            return configurations;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    自动配置第四步:META-INF/spring.factories

    在这里插入图片描述
    之后,Spring Boot加载spring.factories文件的这些类,完成自动配置。

    # Logging Systems
    org.springframework.boot.logging.LoggingSystemFactory=\
    org.springframework.boot.logging.logback.LogbackLoggingSystem.Factory,\
    org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.Factory,\
    org.springframework.boot.logging.java.JavaLoggingSystem.Factory
    
    # PropertySource Loaders
    org.springframework.boot.env.PropertySourceLoader=\
    org.springframework.boot.env.PropertiesPropertySourceLoader,\
    org.springframework.boot.env.YamlPropertySourceLoader
    
    # ConfigData Location Resolvers
    org.springframework.boot.context.config.ConfigDataLocationResolver=\
    org.springframework.boot.context.config.ConfigTreeConfigDataLocationResolver,\
    org.springframework.boot.context.config.StandardConfigDataLocationResolver
    
    # ConfigData Loaders
    org.springframework.boot.context.config.ConfigDataLoader=\
    org.springframework.boot.context.config.ConfigTreeConfigDataLoader,\
    org.springframework.boot.context.config.StandardConfigDataLoader
    
    # Application Context Factories
    org.springframework.boot.ApplicationContextFactory=\
    org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext.Factory,\
    org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.Factory
    
    # Run Listeners
    org.springframework.boot.SpringApplicationRunListener=\
    org.springframework.boot.context.event.EventPublishingRunListener
    
    # Error Reporters
    org.springframework.boot.SpringBootExceptionReporter=\
    org.springframework.boot.diagnostics.FailureAnalyzers
    
    # Application Context Initializers
    org.springframework.context.ApplicationContextInitializer=\
    org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
    org.springframework.boot.context.ContextIdApplicationContextInitializer,\
    org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
    org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
    org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
    
    # Application Listeners
    org.springframework.context.ApplicationListener=\
    org.springframework.boot.ClearCachesApplicationListener,\
    org.springframework.boot.builder.ParentContextCloserApplicationListener,\
    org.springframework.boot.context.FileEncodingApplicationListener,\
    org.springframework.boot.context.config.AnsiOutputApplicationListener,\
    org.springframework.boot.context.config.DelegatingApplicationListener,\
    org.springframework.boot.context.logging.LoggingApplicationListener,\
    org.springframework.boot.env.EnvironmentPostProcessorApplicationListener
    
    # Environment Post Processors
    org.springframework.boot.env.EnvironmentPostProcessor=\
    org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
    org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor,\
    org.springframework.boot.env.RandomValuePropertySourceEnvironmentPostProcessor,\
    org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
    org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor,\
    org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor
    
    # Failure Analyzers
    org.springframework.boot.diagnostics.FailureAnalyzer=\
    org.springframework.boot.context.config.ConfigDataNotFoundFailureAnalyzer,\
    org.springframework.boot.context.properties.IncompatibleConfigurationFailureAnalyzer,\
    org.springframework.boot.context.properties.NotConstructorBoundInjectionFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BeanDefinitionOverrideFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.BindValidationFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.UnboundConfigurationPropertyFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.MutuallyExclusiveConfigurationPropertiesFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.NoSuchMethodFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer,\
    org.springframework.boot.diagnostics.analyzer.PatternParseFailureAnalyzer,\
    org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer,\
    org.springframework.boot.web.context.MissingWebServerFactoryBeanFailureAnalyzer,\
    org.springframework.boot.web.embedded.tomcat.ConnectorStartFailureAnalyzer
    
    # Failure Analysis Reporters
    org.springframework.boot.diagnostics.FailureAnalysisReporter=\
    org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
    
    # Database Initializer Detectors
    org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector=\
    org.springframework.boot.flyway.FlywayDatabaseInitializerDetector,\
    org.springframework.boot.jdbc.AbstractDataSourceInitializerDatabaseInitializerDetector,\
    org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializerDetector,\
    org.springframework.boot.liquibase.LiquibaseDatabaseInitializerDetector,\
    org.springframework.boot.orm.jpa.JpaDatabaseInitializerDetector,\
    org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializerDetector
    
    # Depends On Database Initialization Detectors
    org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
    org.springframework.boot.sql.init.dependency.AnnotationDependsOnDatabaseInitializationDetector,\
    org.springframework.boot.jdbc.SpringJdbcDependsOnDatabaseInitializationDetector,\
    org.springframework.boot.jooq.JooqDependsOnDatabaseInitializationDetector,\
    org.springframework.boot.orm.jpa.JpaDependsOnDatabaseInitializationDetector
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    自动配置讲解完毕。

  • 相关阅读:
    python递归算法
    Unity中PICO中手柄按键返回值
    2-8:Http响应头content-type内容详解
    如何使用Python编写脚本来自动获取和保存网络小说
    hadoop 实现数据排序
    Mysql中校对集utf8_unicode_ci与utf8_general_ci的区别
    Redis内存策略
    Linux权限管理
    【MongoDB】索引 – 文本索引(指定语言)
    C++第二十三弹---深入理解STL中list的使用
  • 原文地址:https://blog.csdn.net/weixin_43982359/article/details/126895773