深入微服务-SpringBoot自动装配原理

366 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第3天,点击查看活动详情

微服务系列文章目录


前言

本系列带着大家深入微服务 Spring体系的各个框架的底层原理


1、首先看下Spring 启动类

@SpringBootApplication
public class AppApplication {
    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }
}

2、SpringBootApplication注解解析

@SpringBootApplication 注解说明本类为SpringBoot的启动类以及配置类

@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

@SpringBootConfiguration:表示为SpringBoot配置类 @EnableAutoConfiguration:开启自动配置功能 @ComponentScan:扫码包,可指定扫码包范围,若未指定则扫描当前启动类包下的类

3、初始化SpringApplication

new SpringApplication(primarySources)

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
   this.resourceLoader = resourceLoader;
   Assert.notNull(primarySources, "PrimarySources must not be null");
   // 将启动类放入primarySources 
   this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
   // 获取web应用类型 (webFlux, servlet)
   this.webApplicationType = WebApplicationType.deduceFromClasspath();
   // 获取 项目 META-INF/spring.factories中org.springframework.context.ApplicationContextInitializer
   setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
   // 获取 项目 META-INF/spring.factories中org.springframework.context.ApplicationListener
   setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
   this.mainApplicationClass = deduceMainApplicationClass();
}

3 调用SpringApplication.run()方法

ConfigurableApplicationContext run(Class<?> primarySource, String... args)

public ConfigurableApplicationContext run(String... args) {
    // 记录SpringBoot启动时间
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   // spring上下文的接口
   ConfigurableApplicationContext context = null;
   Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
   configureHeadlessProperty();
   // 加载META-INF/spring.factories中SpringApplicationRunListener类
   SpringApplicationRunListeners listeners = getRunListeners(args);
   // 发布ApplicationStartedEvent事件
   listeners.starting();
   try {
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
      // 读取配置文件信息
      ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
      configureIgnoreBeanInfo(environment);
      // 打印Spring Banner 
      Banner printedBanner = printBanner(environment);
      // 创建ApplicationContext Spring上下文  
      context = createApplicationContext();
      exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
            new Class[] { ConfigurableApplicationContext.class }, context);
      //预初始化spring上下文
      prepareContext(context, environment, listeners, applicationArguments, printedBanner);
      //  调用ApplicationContext的refresh()方法,刷新容器
      //  refresh()方法会调用我们的bean工厂的后置处理器 invokeBeanFactoryPostProcessors 
      refreshContext(context);
      afterRefresh(context, applicationArguments);
      stopWatch.stop();
      if (this.logStartupInfo) {
         new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
      }
      listeners.started(context);
      callRunners(context, applicationArguments);
   }
   catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, listeners);
      throw new IllegalStateException(ex);
   }

   try {
      listeners.running(context);
   }
   catch (Throwable ex) {
      handleRunFailure(context, ex, exceptionReporters, null);
      throw new IllegalStateException(ex);
   }
   return context;
}

总结

本文主要介绍了SpringBoot 自动装配原理