转载

Spring Boot 原理<1>

在公司使用SSM做了若干个项目,新的项目基本上就是Ctrl+c原来配置。后来公司“紧跟潮流”后面项目改用Spring Boot,由于封装严重,有时出了问题网上都找不到解解决方案,还是乖乖自己捋一捋Spring Boot吧!

Spring Boot是什么

官网解释如下

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Spring Boot的核心是基于Spring构建的,官方对Spring Boot的定位是Build Anything,旨在快速、高效,使用最少的配置构建Java应用。

  • Spring Cloud—————————Coordinate Anything (协调任何东西)
  • Spring Cloud Data Flow—————Connect Everything (连接任何东西)

Spring Boot原理

启动类如下

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
复制代码

SpringApplication是什么类?@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}
)}
)
复制代码
  • @Target(ElementType.TYPE) // 注解的适用范围
  • @Retention(RetentionPolicy.RUNTIME) // 注解的生命周期
  • @Documented // 表明这个注解应该被javadoc记录
  • @Inherited // 子类可以继承该注解
  • @SpringBootConfiguration //表明某个类是springboot的配置类 底层是 @Configration ,本身其实也是一个IoC容器的配置类
  • @ComponentScan //自动扫描并加载符合条件的组件 ,比如@Component和@Repository加载到IoC容器中。
  • @EnableAutoConfiguration开启自动配置功能。

@EnableAutoConfiguration

@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 {};
}
复制代码

核心两项

  • @AutoConfigurationPackage:自动配置包
  • Import(AutoConfigurationImportSelector.class)注解

通过源码发现: @EnableAutoConfiguration也是借助@Import将所有符合自动配置条件的bean定义加载到IoC容器。

@AutoConfigurationPackage也是一个复合式注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
复制代码

AutoConfigurationImportSelector核心方法为selectImports:s

@Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        }
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
                .loadMetadata(this.beanClassLoader);
        AnnotationAttributes attributes = getAttributes(annotationMetadata);
        List<String> configurations = getCandidateConfigurations(annotationMetadata,
                attributes);
        configurations = removeDuplicates(configurations);
        Set<String> exclusions = getExclusions(annotationMetadata, attributes);
        checkExcludedClasses(configurations, exclusions);
        configurations.removeAll(exclusions);
        configurations = filter(configurations, autoConfigurationMetadata);
        fireAutoConfigurationImportEvents(configurations, exclusions);
        return StringUtils.toStringArray(configurations);
    }
复制代码

借助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration可以将所有符合条件的配置都加载到当前IoC容器

原文  https://juejin.im/post/5cb5cf06518825327a059f43
正文到此结束
Loading...